部分数据
1distinct去重
select distinct job from tb_emp;select * from tb_emp where id in (1,2,3);
select * from tb_emp where id between 1 and 5;
select * from tb_emp where name like '__'; #下划线匹配单个字符, %匹配任意多个字符select min(entrydate) from tb_emp;
SELECT AVG(ID) FROM tb_emp;
SELECT SUM(ID) FROM tb_emp;
SELECT max(ID) FROM tb_emp;
2.分组查询
#分组查询可以返回的数据类型
#1.聚合函数
#2.分组字段
SELECT gender,count(*) FROM tb_emp GROUP BY gender;
# 查询入职时间在2015-01-01前的员工, 对结果按照职位分组, 获取员工数量大于等于2的职位
SELECT job,COUNT(*) FROM tb_emp WHERE entrydate <= '2015-01-01' GROUP BY job;
SELECT job,COUNT(*) FROM tb_emp WHERE entrydate <= '2015-01-01' GROUP BY job HAVING COUNT(*) >= 2;
分组前
分组后
3.排序
select * from tb_emp order by entrydate asc ;# desc
select * from tb_emp order by entrydate asc ,update_time desc ;#按照入职时间升序排序, 入职时间相同按照更新时间降序排序
4.分页查询
起始索引 (页码-1) * 每页展示的记录数
select * from tb_emp limit 0,5;#起始索引, 每页展示记录数. 第一页
select * from tb_emp limit 5,5; #第二页select if(gender = 1, '男性' , '女性') as 性别 ,count(*) from tb_emp group by gender;select(case job when 1 then '班主任' when 2 then '讲师' when 3 then '学工主管' when 4 then '教研主管' else '未分配职位' end) as 职位,count(*)
from tb_emp group by job;
一对一关系
一对多关系
多的一方 添加一个字段,关联一的一方的主键