SQL高级教程

1、select TOP

select top 子句用于规定要返回的记录的数目。对拥有数千条记录的大型表来说,是非常有用的。

注意:并非所有的数据库系统都支持select top语句。MySQL支持limit语句来选取指定的条数数据,oracle可以使用rowunm来选取
select * from Websites;

select * from Websites limit 2;

在Websites表中选取前50%的记录:
select top 50 percent * from Websites;

所以该语法用于Microsoft SQL Server数据库。

2 like操作符

like操作符用于在where子句中搜索列中的指定模式。

查询 Websites 表中 name 字段中首字母为G的字段:

select * from Websites
where name like 'G%';

查询 Websites 表中 name 字段中以k结尾的字段:

 select * from Websites
 where name like '%k';

查询 Websites 表中 name 字段中不以 oo 开头和结尾的字段:

select * from Websites
where name like '%oo%';

查询 Websites 表中 name 字段中不包含 oo 的字段:

select * from Websites 
where name not like '%oo%';

4、通配符

通配符可以替代字符串中的任何字符。

描述 通配符
% 替代0个或多个字符
_ 替代一个字符
[A-Za-z] 字符列中的任何单一字符
[^A-Za-z] 不在字符列中的任何单一字符

1》%通配符

例:

在 Websites 表中查询 URL 为 https 的所有网站信息:

select * from Websites
  where url like 'https%';

在 Websites 表中查询 URL 包含“oo” 的所有网站信息:

select * from Websites
  where url like '%oo%';

2》"_"通配符

在 Websites 表中选取 name 以 "G" 开始,然后是一个任意字符,然后是 "o",然后是一个任意字符,然后是 "le" 的所有网站

select * from Websites
    where name like 'G_o_le';

使用[charlist]通配符

MySQL 中使用 regexp 或 not regexp 运算符(或rlike 和 not rlike)来操作正则表达式。

在 Websites 表中选取 name 以“G”、“F”或“s”开始的所有网站:

select * from Websites
    where name regexp '^[GFs]';

在 Websites 表中选取 name 以 A 到 H 字母开头的网站:

select * from Websites
    -> where name like '^[A-H]';

在 Websites 表中选取 name 不以 A 到 H 字母开头的网站:

 select * from Websites
    where name regexp '^[^A-H]';

5、IN 操作符

in 操作符允许你在where子句中规定多个值。

在Websites表中选取 name 为 “Google” 或 “菜鸟教程”的所有网站:

select * from Websites
  where name in ('Google','菜鸟教程'); 

6、between 操作符

between 操作符用于选取介于两个值之间的数据范围内的值。这些值可以是数值、文本或日期。

在 Websites 表中选取 alexa 介于1和20之间的所有网站:

select * from Websites
  where alexa between 1 and 20;

带有 in 的 between 操作符实例

在Websites 表中 选取 alexa 介于 1 到 20 之间 但 country 不为 USA 和 IND 的所有网站:

 select * from Websites
    where (alexa between 1 and 20)
    and country not in ('USA','IND');

7、SQL 别名

通过使用SQL,可以为表名称或列名称指定别名。
基本上,创建别名是为了让列名称的可读性更强。

列的别名实例

在Websites表中指定了两个别名,一个是name列的别名,一个是country列的别名。提示:如果列名称包含空格,要求使用双引号或放括号

select name AS n, country as c
  from Websites;

在Websites表中,我们把三个列(url,alexa,country)结合在一起,并创建一个名为“site_info”的别名:

 select name,concat(url,',',alexa,',',country) as site_info
  from Websites;

表的别名实例

在Websites表中选取“菜鸟教程”的所有访问记录。我们使用“Websites”和“access_log”表,并分别为它们指定表别名“w”和“a”(通过使用别名让sql更简短):

select w.name,w.url,a.count,a.data
    from Websites as w,access_log as a
    where a.site_id=w.id and w.name="菜鸟教程";

8、SQL 连接(join)

SQL join用于把来自两个或多个表的行结合起来。

不同的连接类型

SQL运行join左边加上一些修饰性的关键词,从而形成不同类型的连接,如下表所示:

连接类型 说明
inner join (默认连接方式)只有当两个表都存在满足条件的记录时才会返回行。
left join 返回左表中的所有行,即使右表中没有满足条件的行也是如此。
right join 返回右表中的所有行,即使左表中没有满足条件的行也是如此。
full join 只要其中有一个表存在满足条件的记录,就返回行(mysql不支持)。
self join 讲一个表连接到自身,就像该表是两个表一样。为了区分两个表,在SQL语句中需要至少重命名一个表。
cross join 交叉连接,从两个或者多个连接表中返回记录集中的笛卡尔积。

如果不加任何修饰词,只写 join ,那么默认为 inner join。

select Websites.id,Websites.name,access_log.count,access_log.data
    from Websites
    inner join access_log
    on Websites.id=access_log.site_id;

1)inner join

select Websites.id,Websites.name,access_log.count,access_log.data
     from Websites
     inner join access_log
     on Websites.id=access_log.site_id;

2) left join

select Websites.id,Websites.name,access_log.count,access_log.data
     from Websites
     left join access_log
     on Websites.id=access_log.site_id;

3) right join

select Websites.id,Websites.name,access_log.count,access_log.data
     from Websites
     right join access_log
     on Websites.id=access_log.site_id;

4) full outer join

select Websites.id,Websites.name,access_log.count,access_log.data
     from Websites
      full join access_log
     on Websites.id=access_log.site_id;

或者

 select Websites.id,Websites.name,access_log.count,access_log.data
     from Websites
     left join access_log
     on Websites.id=access_log.site_id
     union
     select Websites.id,Websites.name,access_log.count,access_log.data
     from Websites
     right join access_log
     on Websites.id=access_log.site_id;

由于MySQL不支持full join,可以替换成 left join + right join 来执行

5) union

SQL union 操作符合并两个或多个select 语句的结果。

注意:union 内部的每个select 语句必须拥有相同数量的列。列也必须拥有相似数据类型。同时,每个 select 语句中的列的顺序必须相同。

posted @ 2022-12-05 10:40  空岛迷梦  阅读(381)  评论(0编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css