1.MySQL排序:(order by)
1.升序 :ASC(默认)
语法:select * from 数据表名 order by 字段名 ASC;
2. 降序: DESC
语法:select * from 数据表名 order by 字段名 DESC;
-- 排序 默认升序
select * from mytable2 order by brith ;
-- 降序排序
select * from mytable2 order by brith DESC;
-- 升序排序
select * from mytable2 order by brith ASC;
2.MySQL去重:(union)
语法 :select * from table1 [where子句] union select * from table2 [where子句];
union 后可加all ,也可加distinct。默认为 union distinct。
1.select * from table1 [where子句] union all select * from table2 [where子句];
保留结果集中重复的数据。
2.select * from table1 [where子句] union distinct select * from table2 [where子句];
删除结果集中重复的数据,每条数据只保留一条。
3.MySQL分组:(group by)
1.简单分组并统计每组个数:group by
-- 按照名字分组 并显示每组个数
select name ,count(*) AS '个数' from employee_tbl GROUP BY name ;
2.显示每组signin字段的和:with rollup 再次统计
-- 按照名字分组后再统计signin的总数
select name,sum(signin) from employee_tbl GROUP BY name with rollup;
3.为空值设置name:coalesce()函数
-- coalesce(a,b,c)函数 如果a为空就取b值,b为空就取c值,c为空就返回null
select coalesce (name,'总数') ,sum(signin) from employee_tbl group by name with rollup;
coalesce() 与 ifnull()两函数的区别:ifnull函数只能接收两个参数,意义为:如果第一个为空,则选择第二个参数;而coalesce函数可以接收两个或多个参数,如果第一个为空,则选择第二个参数,第二个为空则选择第三个参数... ...以此类推。
if(expr,值1, 值2):如果expr为true,返回值1;否则,返回值2;