我有一张这样的桌子..
| name | product
---------------------
| Amy | Doll
| Allan | Bat
| Bob | Pen
| Charlie | Eraser
| Charlie | Pencil
| Charlie | Sharpener
| Allan | Ruler
| Anoop | Candy
*******************
我想要每个客户购买的产品数量。输出应按1)按订单顺序购买的产品数量和2)购买相同数量产品的客户按字母顺序排序。
输出:
*******************
| name | number
--------------------
| Charlie | 3
| Allan | 2
| Amy | 1
| Anoop | 1
| Bob | 1
*******************
答案 0 :(得分:1)
使用SELECT name , COUNT(*) AS `number`
FROM your_table
GROUP BY name
ORDER BY `number` DESC, name;
:
CASE
的 LiveDemo
强>