- 筛选条件
- 聚会与分组
- 子查询
- 链接查询
1,筛选条件
等于
=
大于等于
>=
为空
is null
不等于
!= 或 <>
小于
<
非空
is not null
大于
>
小于等于
<=
逻辑运算符
与and
或者 or
非not
其它操作
排序
order by
加上desc
意思是逆序
取数据条数
limit 开始位置,结束位置
去重distinct
例子:select distinct subject_number from grades;
模糊查询 like
通配符 %
(匹配任意多个字符)与 _
(匹配任意单个字符)
2,聚合与分组
2.1聚合函数
- 统计个数
count 例子:select count(*) from students;
- 求和
sum 例子:select sum(age) from students;
- 最大值
max 例子:select max(age) from students;
- 最小值
min 例子:select min(age) from students;
- 平均值
avg 例子:select avg(age) from students;
- 字段全部值
例举出该字段列的所有数据
group_concat 例子:select group_concat(age) from students;
2.2分组查询
在分组的情况下,只能够出现分组字段和聚合函数字段,其它字段没有意义,会报错
group by
例子:
select subject_number from grades group by subject_number; #(分组字段)
例子:
select count(*) from grades group by subject_number; #统计出每项分组的总数(聚合函数字段)
having
#与where相差不远,但是having只允许出现在group by 后面
例子:
select count(*) from grades group by subject_number, grade having grade > 80;
3,子查询
子查询需要取别名,查询语句 as 别名
4,链接查询
内连接
join