Question

I have a table in which I would like to seek out contestants who wrote more than one contest. There are more columns but the only ones I'm looking at are first_name, last_name, school, and contest.

I want to find people in my table who wrote more than one contest, so basically same first_name, same last_name, same school, BUT different contest. There are 3 possible contests.

Also, each contestant has a contestant_id which is the primary key.

EDIT

I should have specified that contest must equal 1 of the 3 contests I specify. The table could have contestants from other contests I'm not looking at and it would be beneficial to list even the duplicates. So if it finds 2 people matching the criteria, it shows both.

EDIT 2

Some example data…

| id | first_name | last_name | school | contest |  
| 01 |   Jane     |    Doe    |  2568  |   1001  |  
| 02 |   Mike     |    Doe    |  2568  |   1003  |  
| 03 |   Jane     |    Doe    |  2568  |   1003  |  
| 04 |   Jane     |    Doe    |  2523  |   1001  |  

In this example id 01 and 03 would match but the others would not because the name is different with 02 and school is different with 04.

Was it helpful?

Solution

SELECT  first_name,last_name,school,contest FROM table 
WHERE contest IN ('blah','mah','wah')
GROUP BY  first_name, last_name, school 
HAVING COUNT(DISTINCT contest)>1

Edit

SELECT * FROM table t JOIN
(SELECT  GROUP_CONCAT(id)as ids,first_name,last_name,school,contest FROM table
WHERE contest IN (1001,1002,1003)
GROUP BY  first_name, last_name, school 
HAVING COUNT(DISTINCT contest)>1)x
ON FIND_IN_SET(t.id,x.ids)>0

FIDDLE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top