一、查询语句
1、查询一张表:
select * from 表名;
2、查询指定字段:
select 字段1,字段2,字段3,… from 表名;
3、where条件查询:
select 字段1,字段2,字段3 from 表名 where 条件表达式;
4、in关键字查询:
select 字段1,字段2 from 表名 where 字段 [not]in(元素1,元素2);
5、between and范围查询:
select 字段1,字段2 from 表名 where 字段 [not]between 取值1 and 取值2;
6、like模糊查询:
select 字段1,字段2,... from 表名 where 字段 [not]like '字符串';
7、空值查询:
select 字段1,字段2,... from 表名 where 字段 is[not] null;
8、and多条件查询:
select 字段1,字段2,... from 表名 where 条件表达式1 and 条件表达式2;
9、or多条件查询:
select 字段1,字段2,... from 表名 where 条件表达式1 or 条件表达式2;
10、distinct去重复查询:
select distinct 字段名 from 表名;
11、对查询结果排序order by:
select 字段1,字段2,... from 表名 order by asc/desc;
12、分组查询group by(不能单独使用):
与group_concat()函数一起使用、与聚合函数一起使用、与having一起使用(显示输出的结果)、与with rollup 一起使用(最后加入一个总和行)
group by 属性名 [having 条件表达式][with rollup];
13、limit分页查询:
select 字段1,字段2,... from 表名 limit 初始位置,记录数;
二、多表连接查询
select 字段列表 from 表1 inner/left/right join 表2 on 表1.字段 = 表2.字段;
三、子查询(嵌套查询)
子查询:查询多次,多个select语句
注意:第一次的查询结果可以作为第二次的查询的条件或表名使用
子查询中可以包含IN、NOT IN、ANY、ALL、EXISTS、NOT EXISTS等关键字,还可以包含比较运算符= 、 !=、> 、<等
1、作为表名使用
select * from (select * from person) as 表名;
2、any关键字
select ... from ... where a > any(...);
3、all关键字
select ... from ... where a > all(...);
4、some关键字
select ... from ... where a > some(...);