首页 > 其他 > 详细

having和where的区别

时间:2017-05-21 15:54:05      阅读:218      评论:0      收藏:0      [点我收藏+]

区别:

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的区别

原文:http://www.cnblogs.com/healy/p/6884923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!