SQL高级

1. SELECT TOP子句

1. SQL Server | MS Access 语法
sekect top number | percent 字段名 from 表名;
2. MySQL 和 Oracle 中的 select top 等价
select * from school limit 8;
select 字段名 | * from 表名 limit number;
select 字段名 | * from 表名 where rownum <= number;
3. MySQL select top percent 实例

在 Microsoft SQL Server 数据库中可执行

select top number percent * from 表名;

2. LIKE语法

select 表名 from where 字段名 like pattern; 
例: select school from where name 'G%';G开头的所有名字

[%]在模式的前后定义通配符

select * from school where name like  '%k';  以k结尾
select * from school where name like  'c%';  以c开头
select * from school where name like  '%oo%';  含有oo
select * from school -> where name like  '%00%';  不含有oo的所有数据

3. 通配符

1. % 替代 0 个或多个字符
select * from 表名 where 字段名 like  '值';
select * from school where name like 't%';
2. _ 替代一个字符
select * from 表名 where 字段 like '_值';  值的首字符用_代替,
select * from school where name like '_ongzhu'; 也可以_ on_zh_
3. [charlist] 字符列中的任何单一字符
select * from 表名 where 字段名 regexp '^[字符]';
select * from school where name regexp '^[ty]';
select * from school where name regexp '^[a-z]';  选取 name以 a 到 z 字母开头的名字
4. [^charlist] 或 [!charlist] 不在字符列中的任何单一字符
select * from school where name regexp '^[^a-z]';  选取 name 不以 A 到 H 字母开头的名字

4. 操作符

1. [in]

允许在 where 子句中规定多个值

select * from 表名 where 字段名 in (values1,values2,...);
select * from school where remark in ('keai','eef');
2. [between]

选取介于两个值之间的数据范围内的值

select * from 表名 where 字段名 between value1 and value2;
select * from school where number  between 700 and 900;

3. [not between]

选取不在这个范围内的值

select * from 表名 where 字段名 not between value1 and value2;
select * from school where number  not between 700 and 900; 
4. [带有in 的 between]
select * from 表名 where (字段名 between value and value ) and not 字段名 in ( 'value','value');
select * from school where (字段名 between value and value ) and not 字段名 in ( 'value','value');
5. [带有文本值的not between]
select * from 表名 where 字段名 not between '值' and '值';
select * from school where name not between 'a' and 'h';
6. [带有日期值的between]
select * from 表名 where date between '日期值' and ‘日期值’;
select * from dates where date bewttn '2017-03-25' and '2017-04-19';
7. [union]

操作符合并两个或多个 SELECT 语句的结果

select 字段名 from 表1  union select 字段名 from 表2 order by 字段名;  三个字段名为同名
select country from website union select country from apps order by country;
8. [union all]

选取所有的country

select 字段名 from 表1  union all select 字段名 from 表2 order by 字段名;  三个字段名为同名
select country from website union all select country from apps order by country;
9. [带有where 的 union all]

选取所有的某一个值

select 字段名 , 字段名 from 表1  where 字段名='值' union all select 字段名,  字段名 from 表2 where 字段名='值' order by 字段名;  三个字段名为同名
select country, name from website where country='CN' union all select country, appname from apps where country='CN' order by country;

5. SQL别名

1. [列别名]
select  字段名 as 别名 from 表名;
select count as number from dates;
多列合一列如下:
select name, concat(字段名, ',' , 字段名,.....)as 别名 from 表名;
select name, concat (name, ',' , count) as nc from dates;
2. [表别名]
select 别名.字段名,别名.字段名,别名.字段名,... from 表名 as 别名 where 别名.字段名='值';
select d.id, d.name, d.count, d.date from dates as d where d.name='safari';
[下面的情况下使用别名很有用:]
  1. 在查询中涉及超过一个表
  2. 在查询中使用了函数
  3. 列名称很长或者可读性差
  4. 需要把两个列或者多个列结合在一起

3. SQL JOIN

1. 可以使用的不同的 SQL JOIN 类型:

** INNER JOIN**
如果表中有至少一个匹配,则返回行

select 表名.字段名,  表名.字段名,  表名.字段名, ... from 表名1 inner join 表名2 on 表名1.字段名=表名2.字段名;
select dates.id, dates.name, dates.date, log.alexa from dates inner join log on dates.id=log.sid;
                                          or
select dates.id, dates.name, dates.date, log.alexa from dates  join log on dates.id=log.sid;    

LEFT JOIN
即使右表中没有匹配,也从左表返回所有的行

select 表名.字段名,  表名.字段名,  表名.字段名, ... from 表名1 left join 表名2 on 表名1.字段名=表名2.字段名;
select dates.id, dates.name, dates.date, log.alexa from dates left join log on dates.id=log.sid;
                                    or
select dates.id, dates.name, dates.date, log.alexa from dates left outer join log on dates.id=log.sid;

** RIGHT JOIN**
即使左表中没有匹配,也从右表返回所有的行

select 表名.字段名,  表名.字段名,  表名.字段名, ... from 表名1 right join 表名2 on 表名1.字段名=表名2.字段名;
select dates.id, dates.name, dates.date, log.alexa from dates right join log on dates.id=log.sid;
                                    or
select dates.id, dates.name, dates.date, log.alexa from dates right outer join log on dates.id=log.sid;

FULL OUTDER JOIN
只要其中一个表中存在匹配,则返回行

MySQL中不支持 FULL OUTER JOIN,可以在 SQL Server 测试以下实例。

select 表名.字段名,  表名.字段名,  表名.字段名, ... from 表名1 full outer join 表名2 on 表名1.字段名=表名2.字段名 order by 表2.字段名 desc;
select  dates.name, dates.date, log.count from dates full outer join log on dates.id=log.sid order by log.count desc;

4. INSERT INTO SELECT

复制A表中的数据到B表

insert into A表 (字段名,字段名,...) select 字段名,字段名 from B表;
insert into website (name, country) select appname, country from apps;

你可能感兴趣的:(SQL高级)