统计每种商品数量的几种写法

最常见的一种

select count(*) from goods group by goods_id;

商品数量少的话可以用下面两种写法

select sum(IF(goods_id=1,1,0)) as a,sum(goods_id=2,1,0) as b from goods;

select count(IF(goods_id=1 or null)) as a,count(goods_id=2 or null) as b from goods;

posted @ 2017-02-09 14:19  李修远  阅读(982)  评论(0编辑  收藏  举报