having和where的区别

区别:

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

 

posted @ 2017-05-21 15:08  Howie_Tang  阅读(434)  评论(0编辑  收藏  举报