1. 程式人生 > >having 和where 的區別

having 和where 的區別

having 關鍵字 可以接聚合函式 出現在分組(group by)之後(只能在分組之後用)
where 關鍵字 它是不可以接聚合函式,出現在分組之前 --都是做條件的過濾

根據no分組。分組後統計每組商品的平均價格 並且價格>60

select     no,avg(price)   
from  prouduct   group   by   no
having   avg (price) >60

編寫順序
S…F…W…G…G…O
select … from … where …group by … having … order by

執行順序
from… where… group by… having… select … order by

delete 與 truncate 區別
delete from 表名 where
truncate table 表名 :先刪除 表,再重建表