筛选条件
- 比较运算符:
- 等于: = (注意!不是==)
- 大于等于: >=
- IS NULL
- 不等于: != 或 <>
- 小于:<
- IS NOT NULL
- 大于: >
- 小于等于:<=
- 逻辑运算符:
- AND
- OR
- NOT
- 排序:
- order by
- select * from table order by id
- 根据id排序
- asc / desc
- asc:正序
- desc:倒序
- 默认为正序
- 多条件排序: 在order by 后面用',' 再加order by
- select * from table order by id
- order by
- 限制:
- limit:
- select * from table limit 1
- 选择一条(默认选择第一条)
-
- select * from table limit start,count
- 从start的位置开始选择count条
- select * from table limit1,2
- 从第一条(从0开始排序)开始选择2条
- select * from table limit start,count
- 去重:
- select distinct * from table
- 讲表里的数据去重后返回
- select distinct * from table
- 模糊查询:
- select * from student where name like '李%'
- 查询表里的名字以'李'开头的数据
- '%':
- 匹配任意多个字符
- '_':
- 匹配一个字符
- 范围查询:
- between:
- select * from table where id between 2 and 4
- 选择id为2到4之间的数据(包含2和4,左闭右闭)
- in:
- select * from table where id in (1,2,3)
- 选择id为1,2,3的数据
聚合与分组
- count:统计个数
- select count(id) from table
- 统计id的个数
- select count(id) from table
- avg:求平均数
- select avg(id) from table
- 统计id的平均数
- max:求最大值
- select max(id) from table
- min: 求最小值
- select min(id) from table
- sum: 求和
- select sum(id) from table