我有一个带有数据库表的消息系统,如下所示。 UID是用户ID,sid是发送消息的商店ID。
UID SID Content
1 10 "blah"
1 11 ...
2 10 ...
3 12 ...
3 12 ...
3 10 ...
我想将用户按每个商店收到的邮件数量分组。所以输出将是
UID NumUniqueSIDs
1 1 (corresponds to uid = 1, sid = 10)
1 1 (corresponds to uid = 1, sid = 11)
2 1 (corresponds to UID = 2, sid = 10)
3 2 (corresponds to UID = 3, sid = 12)
3 1 (corresponds to UID = 3, sid = 10)
我无法提出完成此操作的查询。有谁知道如何做到这一点?
答案 0 :(得分:1)
以下查询会生成正确的结果:
SELECT uid, count(sid) as NumUniqueSIDs from Messages group by uid, sid