我有一个非常简单的提交表:userId,submissionGuid
我想在表格中选择包含10个以上提交内容的所有用户的用户名(简单的内部联接来获取)。
我会使用嵌入式查询和分组来计算提交内容......但是有更好的方法吗(没有嵌入式查询)?
谢谢!
答案 0 :(得分:5)
这是最简单的方法,我相信:
select userId
from submission
group by userId
having count(submissionGuid) > 10
答案 1 :(得分:1)
select userId, count(*)
from submissions
having count(*) > 10
group by userId
答案 2 :(得分:1)
SELECT
username
FROM
usertable
JOIN submissions
ON usertable.userid = submissions.userid
GROUP BY
usertable.username
HAVING
Count(*) > 1
*假设您的“用户”表是呼叫使用的,并且它有一个名为“UserName”的列
答案 3 :(得分:0)
我认为正确的查询是这个(SQL Server):
SELECT s.userId, u.userName
FROM submission s INNER JOIN users u on u.userId = s.userId
GROUP BY s.userId, u.username
HAVING COUNT(submissionGuid) > 10
如果您没有HAVING条款:
SELECT u.userId, u.userName
FROM users u INNER JOIN (
SELECT userId, COUNT(submissionGuid) AS cnt
FROM submission
GROUP BY userId ) sc ON sc.userId = u.userId
WHERE sc.cnt > 10
答案 4 :(得分:0)
选择userid,count(submissionGUID)为submitCount
来自提交的
按用户ID分组,submitCount
拥有submitCount> 10