【发布时间】:2014-01-22 14:31:19
【问题描述】:
以下查询:
SELECT COUNT(*) AS count, items.category, customers.sector
FROM customers
LEFT JOIN items ON items.customer_id = customers.id
GROUP BY items.category, customers.sector
ORDER BY customers.sector ASC
给我这个结果:
| count | category | sector |
|-------|-----------------------|--------------|
| 3 | A-Frames & Trolleys | Automotive |
| 4 | Suction Mounts | Automotive |
| 1 | Hand Cups | Automotive |
| 103 | Glazing Tools | Construction |
| 2 | A-Frames & Trolleys | Construction |
| 2 | Suction Mounts | Construction |
|_______|_______________________|______________|
我希望扇区列是唯一的,并显示计数最多的类别 例如:
| count | category | sector |
|-------|-----------------------|--------------|
| 4 | Suction Mounts | Automotive |
| 103 | Glazing Tools | Construction |
|_______|_______________________|______________|
谢谢
【问题讨论】:
-
所以您想要每个
sector的MAX(COUNT())? -
是的,这就是我所追求的
标签: mysql count group-by distinct