我有一个包含三列的数据库表(注释):id、ticket 和 user。
我需要显示每个用户输入了多少条注释,以及这些注释被添加到多少张唯一票证(按用户分组)。例如,John Doe 在 50 张独特的票中输入了 100 个音符,而 Jane Doe 在 65 张独特的票中输入了 70 个音符。我对它们被添加到哪些门票不感兴趣,只是有多少。
这是我目前所拥有的。
select count(id) count
from notes
group by user
是否可以在同一查询中获取唯一门票的数量?
最佳答案
在计数中使用distinct
:
select
user,
count(id) note_count,
count(distinct ticket) ticket_count
from notes
group by user
关于MySQL查询计算两个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16106576/