
mysql having用法介绍
今天遇到一个问题,想查询订单里面某个商品购买次数超过30次的商品,想到了having
如下sql:
1.要得到商品pro_id对应个数:
select pro_id,count(pro_id) from new_order where status = 2 group by pro_id having count(pro_id)>30
查询出来
| pro_id | count(pro_id) | +--------+-----------+ | 1 | 40 | | 2 | 50 | | 4 | 31 | +--------+-----------+ 3 rows in set
2.还有用法就是查询总金额超过多少的商品,下面是查询金额超过30000的例子:
select pro_id,sum(money) from new_order where status = 2 group by pro_id having sum(money)>30000
查询出来
+--------+------------+ | pro_id | sum(money) | +--------+------------+ | 1 | 48000.00 | | 4 | 36000.00 | +--------+------------+ 2 rows in set