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

having和where的區別

字段 錯誤 沒有 avi 語句 font count price ()

區別:

where:語句條件字段,必須是“數據表中存在的”字段

having:語句條件字段 必須是查詢結果集中存在的字段

having()設置sql語句查詢條件

group by 就使用having

where 和 having都可以設置查詢條件,兩種在某些場合可以通用

where:條件字段必須是“數據表” 存在字段

having:條件字段必須是“結果集”中的字段

一、兩者可以通用

select * from goods where goods_price > 1000;

select * from goods where goods_price >1000;

二、只能用where (不能用having)

select goods_id,goods_name from goods where goods_price > 1000;

select goods_id,goods_name from goods having goods_price > 1000;//錯誤,因為結果集中沒有goods_price

三、只能用having(不能用where)

select goods_brand_id,count(*) as cnt from goods having cnt > 3;

selecct goods_brand_id,count(*) as cnt from goods where
cnt > 3;//錯誤,因為表中沒有cnt

having和where的區別