having与where的异同


having与where类似,可以筛选数据,where后的表达式怎么写,having后的表达式就怎么写

where针对表中的列发挥作用,查询数据

having针对查询结果中的列发挥作用,筛选数据

#查询本店商品价格比市场价低多少钱,输出低200元以上的商品

select goods_id,good_name,market_price - shop_price as s from goods having s>200 ;

#如果用where的话则是:

select goods_id,goods_name from goods where market_price - shop_price > 200;

#同时使用where与having

select cat_id,goods_name,market_price - shop_price as s from goods where cat_id = 3 having s > 200;

你可能感兴趣的:(MySql,mysql)