1.基础查询

where 条件查询

select * from table_name where 条件;

# 比较运算符查询
等于: =
大于: >
大于等于: >=
小于: <
小于等于: <=
不等于: != 或 <>

select * from students where id > 3;
select * from students where name != 'xx';
select * from sutdents where gender = 0;

# 逻辑运算符查询
and
or
not

select * from students where id > 3 and gender=0;
select * from students where id < 4 or is_delete=0;
select * from students where not (age >=10 and age <=15); 

# 模糊查询
like是模糊查询关键字
%表示任意多个任意字符
_表示一个任意字符

select * from students where name like '黄%';
select * from students where name like '黄_';
select * from students where name like '黄%' or name like '%靖';

# 范围查询
between .. and .. 表示在一个连续的范围内查询
in 表示在一个非连续的范围内查询

select * from students where id between 3 and 8;
select * from students where (not id between 3 and 8) and gender='男';

# 空判断查询
判断为空使用: is null
判断非空使用: is not null

select * from students where height is null;

排序查询

# 先按照列1进行排序,如果列1的值相同时,则按照列2 排序,以此类推
select * from 表名 order by 字段1 asc|desc [,字段2 asc|desc, ...]

select * from students where gender =1 and is_delete=0 order by id desc;
select * from students order by age desc,height desc;

分页查询

limit 是分页查询关键字
start 表示开始行索引,默认是0
count 表示查询条数

select * from 表名 limit start,count;

select * from students where gender=1 limit 3;
# 已知每页显示m条数据,查询第n页显示的数据
select * from students where m*(n-1),m;

聚合查询

count(col): 表示求指定列的总行数
max(col): 表示求指定列的最大值
min(col): 表示求指定列的最小值
sum(col): 表示求指定列的和
avg(col): 表示求指定列的平均值

select count(*) from students;
select max(id) from students where gender=1;
select min(height) from students where gender=1;
select sum(money) from students;
select avg(age) from students;

# todo:
ifnull

分组查询
分组查询就是将查询结果按照指定字段进行分组,字段中数据相等的分为一组。

列名: 是指按照指定字段的值进行分组。
HAVING 条件表达式: 用来过滤分组后的数据。
WITH ROLLUP:在所有记录的最后加上一条记录,显示select查询时聚合函数的统计和计算结果

select * from table_name where 条件 group by 字段 [having 条件表达式][with rollup]

# group by的使用
select gender from students group by gender;
# 根据name和gender字段进行分组
select name, gender from students group by name, gender;

# group_concat()使用
# group_concat(字段名): 统计每个分组指定字段的信息集合,每个信息之间使用逗号进行分割
select gender,group_concat(name) from students group by gender;

# group by + 聚合函数的使用
select gender,avg(age) from students group by gender;

# having的使用
select gender,count(*) from students group by gender having count(*) > 2;

# with rollup的使用
-- with rollup的作用是:在最后记录后面新增一行,显示select查询时聚合函数的统计和计算结果
-- 根据gender字段进行分组,汇总总人数
select gender,count(*) from students group by gender with rollup;
-- 根据gender字段进行分组,汇总所有人的年龄
select gender,group_concat(age) from students group by gender with rollup;

2.连接查询

内连接查询
查询两个表中符合条件的共有记录

select 字段 from 表1 inner join 表2 on 表1.字段1 = 表2.字段2

select * from students as stu inner join classes as cls on stu.cls_id;

左连接查询
以左表为主根据条件查询右表数据,如果根据条件查询右表数据不存在使用null值填充

select 字段 from 表1 left join 表2 on 表1.字段1 = 表2.字段2

select * from students as s left join classes as c on s.cls_id = c.id;

右连接查询
以右表为主根据条件查询左表数据,如果根据条件查询左表数据不存在使用null值填充

select 字段 from 表1 right join 表2 on 表1.字段1 = 表2.字段2

select * from students as s right join classes as c on s.cls_id = c.id;

自连接查询
左表和右表是同一个表,根据连接查询条件查询两个表中的数据。

-- 查询省的名称为“山西省”的所有城市
select c.id, c.title, c.pid, p.title from areas as c inner join areas as p on c.pid = p.id where p.title = '山西省';

3.子查询

在一个 select 语句中,嵌入了另外一个 select 语句, 那么被嵌入的 select 语句称之为子查询语句,外部那个select语句则称为主查询.

主查询和子查询的关系
1.子查询是嵌入到主查询中
2.子查询是辅助主查询的,要么充当条件,要么充当数据源
3.子查询是可以独立存在的语句,是一条完整的 select 语句

-- 查询大于平均年龄的学生
select * from studnets where age > (select avg(age) from students;)

-- 查询学生在班的所有班级名字
select name from classes where id in (select cls_id from students where cls_id is not null);

-- 查找年龄最大,身高最高的学生
select * from students where age, height (select max(age), max(height) from studnets);