我有一张桌子programparticipants
。我目前正在成功查询count(name) > 1
的ID。我现在需要的是查询属于那些count(name) > 1
。
示例,当前返回的数据结果:
ID count(name)
1 2
3 4
4 3
示例,需要数据结果:
ID name
1 nm1
1 nm3
3 nm2
3 nm3
3 nm4
3 nm7
4 nm5
4 nm8
4 nm9
答案 0 :(得分:51)
select count(id), name
from programparticipants
group by name
having count(id) > 1
答案 1 :(得分:6)
你可以用这个:
SELECT
(SELECT name FROM participants WHERE id=p.participantid) AS name
FROM
programparticipants AS p
WHERE
.... (the part where you find count(name)>1)
答案 2 :(得分:4)
我认为GROUP BY and HAVING就是你想要的。
答案 3 :(得分:2)
select id, Name
from programparticipants
where id in ( <your current query selecting only id here> )
答案 4 :(得分:1)
认为你想看看分组:
select id, name, count(name) from table group by 2,1 having count(name) = 2;
您可以将= 2替换为&gt; 1取决于你想要的任何东西(标题说= 2,问题说&gt; 1)
答案 5 :(得分:1)
SELECT id, Name
FROM programparticipants
GROUP BY id, Name
HAVING COUNT(*) > 1