- 解题思路
- 过于简单此题,只需要拆分这句话就能得到本题考点👇
“每款的SPU(货号)数量,并按SPU数量降序排序”
- “每款”,表示要使用分组函数GROUP BY按款式,即GROUP BY style_id;
- “SPU(货号)数量”, 表示要用COUNT函数来统计item_id或style_id,即COUNT(style_id) AS SPU_num 或COUNT(item_id) AS SPU_num;
- 最后 “按SPU数量降序排序” ,表示用到ORDER函数排序,即ORDER BY SPU_num DESC; 组合起来就是答案。
- 参考代码
SELECT style_id, COUNT(style_id) AS SPU_num FROM product_tb GROUP BY style_id ORDER BY SPU_num DESC;
或
SELECT style_id, COUNT(item_id) AS SPU_num FROM product_tb GROUP BY style_id ORDER BY SPU_num DESC;