gpt4 book ai didi

php - 你能在 mysql 中对不同的列进行分组和计数吗?

转载 作者:行者123 更新时间:2023-11-29 05:12:04 25 4
gpt4 key购买 nike

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

User    |   item  | category____________________________Bill    |   car   | newBill    |   truck | usedBill    |   boat  | newBill    |   house | oldSue     |   car   | usedSue     |   truck | usedBill    |   bike  | newSue     |   viola | used

I'd like the results

User category__________________________Bill|  new   |  3Bill|  used  |  1Bill|  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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com