用户有一个列表,其中包含不同类别的项。
我想知道用户在每个类别中有多少项。

User    |   item  | category
____________________________
Bill    |   car   | new
Bill    |   truck | used
Bill    |   boat  | new
Bill    |   house | old
Sue     |   car   | used
Sue     |   truck | used
Bill    |   bike  | new
Sue     |   viola | used

I'd like the results

User category
__________________________
Bill|  new   |  3
Bill|  used  |  1
Bill|  sold  |  1

I've tried

$SQL = "SELECT COUNT(DISTINCT CATEGORY) FROM LIST WHERE USER=Bill";

但这并没有让我走多远。
谢谢

最佳答案

您需要使用group by语句:

select user, category, count(distinct item)
from mytable
where user = 'Bill'
group by user, category

关于php - 您可以在mysql的不同列上进行分组和计数吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38087609/

10-12 04:42