sql语句单表查询优先级
from > where > group by > having > select > distinct > order by > limit
from
where
group by
例:
select count(1) from table_name group by c_name;
select count(1),group_concat(c_name) from table_name group by c_name; # group_concat(c_name) 关联显示分组之前的字段
having 二次筛选,后面的字段只能是分组(group by)之后的字段
例:
select column_name,count(1) from table_name group by column_name having count(1)>1;
此处需要用having 不能用where,因为where的优先级大于group by
select
distinct 去重
order by
例:
select * from table_name order by c_name; 升序
select * from table_name order by c_name asc; 升序
select * from table_name order by c_name desc; 降序
select * from table_name order by c_name asc,c_name desc;先按照左边的排序有值相等的情况下再按右边的排
limit
例:
select * from table_name limit 0,3; 0是起始位置(索引) 3是步长;