[英]MS ACCESS SQL: How to do a nested count
I am trying to count the number of messages userid = 1 did and then COUNT the number of messages of all the other userid and print out the names of the people where the number of messages = the number of messages userid = 1 has. 我试图计算userid = 1所做的消息数,然后计算所有其他userid的消息数,并打印出消息数= userid = 1的消息人的姓名。
So I need to do a nested count. 所以我需要做一个嵌套计数。 I have 2 tables for this. 我有两张桌子。 Messages and User. 消息和用户。
For example: 例如:
SELECT COUNT(*)
FROM MESSAGES
WHERE user_id = 1
And the result is 2. Then I need 结果是2。然后我需要
SELECT COUNT(*)
FROM MESSAGES
WHERE COUNT(*) = 2
How do I do a nested count for this so I can do something like 我如何为此做一个嵌套计数,所以我可以做类似的事情
SELECT USER.user_name
FROM MESSAGES INNER JOIN USER ON MESSAGES.user_id = USER.user_id
WHERE COUNT(*) = (
SELECT COUNT(*)
FROM MESSAGES
WHERE user_id = 1
)
The above example doesn't work for me. 上面的示例对我不起作用。 I am using MsAccess. 我正在使用MsAccess。 Any help will be appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。
Try using HAVING
syntax 尝试使用HAVING
语法
SELECT USER.user_name
FROM MESSAGES INNER JOIN USER ON MESSAGES.user_id = USER.user_id
GROUP BY MESSAGES.user_id, USER.user_name
HAVING COUNT(*) = (
SELECT COUNT(*)
FROM MESSAGES
WHERE user_id = 1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.