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; # 分頁
sql語句之單表查詢