mysql ---排序、分组、并集操作、distinct
1、order by 排序
select * from employee order by salary ASC limit 10; //查询出按salary排序最低的的10,名员工
2.distinct 获取不重复的唯一值
select distinct first_name from employee;
3.group by 分组统计
select first_name,count(*) cnt from employee group by first_name order by cnt DESC; // 按照first_name分组,并根据first_name出现次数按降序排列
可以在group by 语句后添加 having 字句,对聚集结果进行筛选
select first_name,count(*) cnt from employee group by first_name having cnt > 10 order by cnt desc;
4.union 和 union all
select * from a union select * from b;
select * from a union all select * from b;
上面两个语句都是执行并集操作,但union all 比union更快,union 实际上是union distinct,在进行表连接的时候会筛选掉重复记录,所以在表连接后会对产生的结果集进行排序运算,删除重复的记录再返回结果。