Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query need to get names where count(id) = 2

Tags:

sql

I have a table programparticipants. I am currently successfully querying the IDs where count(name) > 1. What I need now is to query the names that belong to those IDs where count(name) > 1.

Example, data result currently being returned:

ID     count(name)
1      2
3      4
4      3

Example, data result needed:

ID     name
1      nm1
1      nm3
3      nm2
3      nm3
3      nm4
3      nm7
4      nm5
4      nm8
4      nm9
like image 234
mattgcon Avatar asked Sep 15 '09 22:09

mattgcon


2 Answers

select count(id), name 
from programparticipants 
group by name 
having count(id) > 1
like image 159
kragan Avatar answered Sep 28 '22 03:09

kragan


You may use this:

SELECT 
   (SELECT name FROM participants WHERE id=p.participantid) AS name
FROM
   programparticipants AS p
WHERE
   .... (the part where you find count(name)>1)
like image 43
Cem Kalyoncu Avatar answered Sep 28 '22 02:09

Cem Kalyoncu