sql语句之查询操作

 

语法顺序

select distinct 字段1,字段2,字段3 from 库.表

  where 条件    

  group by 分组条件  

  having 过滤    # 执行顺序的话,到这步会返回运行select语句,之后再运行order by

  order by 排序字段

  limit   n;  # 限制打印到屏幕上的条数

 

where 须在group by之前,不可用聚合函数

hanving需在group by之后,可用聚合函数

select 后面可以用聚合函数,如select count(id) from employee

 

执行顺序

from->where->group by->having->distinct->order by->limit

 

# group by

设置 mysql> set global sql_mode="ONLY_FULL_GROUP_BY";  #select只能取分组的字段

select post from employee group by post;而不能select * from employee group by post;

 

# group_concat

把group by出来的记录的某个字段值连起来,如 select post,group_concat(name) from employee group by post;

 

# having

select * from employee group by post having count(id)<2 ;

 

# order by

select * from employee order by age desc,id asc;

 

# limit

select * from employee limit 0,5;

select * from employee limit 5,5;  # 分页

 

posted @ 2018-03-14 11:02  Claire_xu  阅读(343)  评论(0编辑  收藏  举报