一、UNION的用法
UNION操作符主要用于两个以上的select的结果集合并到一个结果集,默认为删除重复结果!
两个参数:1.distinct-删除重复的结果 2.ALL-保留全部结果集
例子:
两张原始数据表websites、apps。从两张表中选取不同的country
select country from websites UNION select country from apps order by country;
从两张表中取出所有country:
select country from websites UNION[ALL] select country from apps order by country;
二、ORDER BY的用法:
order by 用于将查询的结果排序,可以根据条件按升序或降序排列。
实例:根据日期进行排序
SELECT * from runoob_tbl ORDER BY submission_date ASC; ---按时间升序排列
SELECT * from runoob_tbl ORDER BY submission_date DESC;---按时间降序排列
三、分组GROUP BY的用法
group by根据一个或多个列对结果集进行分组,可以搭配COUNT, SUM, AVG等函数使用
用例:一张员工表,根据姓名分组并得到每个人有多少条记录
select name, count(*) from employee_tb group by name;
返回结果:
WITH ROLLUP的使用:WITH ROLLUP 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)
例如:按照姓名分组并统计每个人singin次数
select name, sum(singin) as singin_aount from employee_tb group by name with rollup;
返回结果: