数据表格,创造数据都在上篇文章MySQL常用语句里,可以在我的栏目里看到。
这篇写MySQL分组数据
以供应商id进行分组
select vend_id,count(*) as num_prods from products group by vend_id;
过滤分组
列出具有两个以上产品且其价格大于等于4的供应商
select vend_id,count(*) as num_prods from products where prod_price>=4 group by vend_id having count(*)>=2;
列出具有两个以上产品的供应商
select vend_id,count(*) as num_prods from products group by vend_id having count(*)>=2;
以订单号进行分组,显示包含3个或3个以上的物品的订单号和订购物品的数目,以订购物品的数目和订单号进行排序
select order_num,count(*) as items from orderitems group by order_num having count(*)>=3 order by items, order_num;