壹:
where 后面不能跟聚合函数 (sum,avg,count,max,min)
having 后面可以跟
贰:
where 和 having 都能用:
- select goods_price,goods_name from sw_goods where goods_price>100
- select goods_price,goods_name from sw_goods having goods_price>100
原因: goods_price 作为条件也出现在了查询字段中.
只能用 where:
- select goods_name,goods_number from sw_goods where goods_price>100
- select goods_name,goods_number from sw_goods where goods_price>100(X)
原因: goods_price 作为筛选条件没有出现在查询字段中, 所以就会报错.
只能用 having 的:
select goods_category_id,avg(good_price) as ag from sw_goods group by goods_category having ag>1000
select goods_category_id,avg(goods_price) as ag from sw_goods where ag>1000 group by goods_category(X) 报错, 这个表里没有这个 ag 这个字段.
来源: http://www.bubuko.com/infodetail-3333193.html