SQL分组
作者:互联网
建立分组:
SELECT vend_id,COUNT(*) AS num_prods FROM products GROUP BY vend_id;
vend_id是Vendor_id的缩写:供应商id,products是一个存储商品信息的表。
通过GROUP BY语句,我们可以按照供应商分组,然后组内计数。
SELECT a.prod_id,a.prod_name,a.prod_price,a.prod_desc,b.num_prods FROM products a join ( SELECT vend_id,count(*) as num_prods FROM products group by vend_id ) b on a.vend_id=b.vend_id where num_prods>2
查询提供2个以上产品的供应商的产品。
标签:prods,vend,num,分组,SQL,products,prod,id 来源: https://www.cnblogs.com/liuguangshou123/p/13906540.html