简体   繁体   中英

mysql select distinct where in multiple rows

Consider a table, zGameParticipants with these two columns and values

GameID | ParticipantID
1      | 10
1      | 11
2      | 11
2      | 12
3      | 13
3      | 14
3      | 15

I would like to select eg the distinct GameID that has ParticipantID 10 AND 11. For obvious reasons I cannot just do like this

SELECT 
DISTINCT GameID
FROM zGameParticipants
WHERE ParticipantID IN ($ParticipantIDs)

where $ParticipantIDs is an array with ParticipantIDs.

There can be n ParticipantIDs, so adding n ParticipantID to an array and bind as parameters may work somehow. Otherwise a nested select may be what I am looking for, but I can't get my head around this.

if all you want is a unique game no matter the number of participants... aka 1 row per game

then just add GROUP BY GameID

this is assuming that $ParticipantIDs is a comma separated list like this

SELECT GameID
FROM zGameParticipants
WHERE ParticipantID IN (13,14,15)
GROUP BY GameID

if you dont know how to make it a comma separated list then use implode in php like this

$commaList = implode(', ', $ParticipantIDs);

then you can put it in your query

SELECT GameID
FROM zGameParticipants
WHERE ParticipantID IN ($commaList)
GROUP BY GameID

I would recommend you look into making it a parameterized query and you bind the parameter into the IN() statement

EDIT:

from your comments it seems like you want all of the participants to be in a game for that row to be returned. to do this you can just do something like this

$counter = count($ParticipantIDs);
$commaList = implode(', ', $ParticipantIDs);

SELECT GameID
FROM zGameParticipants
WHERE ParticipantID IN ($commaList)
GROUP BY GameID
HAVING COUNT(DISTINCT ParticipantID) = $counter

Adding to @John Ruddell

Example

Input params : ParticipantID in range (13,14,15)

SELECT GameID
FROM zGameParticipants
WHERE ParticipantID IN (13,14,15)
GROUP BY GameID
HAVING COUNT(DISTINCT ParticipantID) = 3 // here count of ParticipantID`s range

I added HAVING=3 ,because it guarantees that GameID has 13 AND 14 AND 15.

I believe you are looking for the GameIDs that have ALL the participants listed in the array. (And that is why you can't use the SQL query that you posted because that would select the GameIDs that have ANY of the participants)

If that is the case, one of the ways to solve this would be to generate your query dynamically.

$ParticipantIDs = array(10,11,12,13,14,15);
$selectClause = "SELECT 
DISTINCT Table0.GameID\n";
$joinClauses = "";
$whereClauses = "WHERE\n";
$i = 0;
foreach($ParticipantIDs as $participantID)
{
    if ($i == 0)
    {
        $joinClauses .= "FROM zGameParticipants AS Table$i\n";
    }
    else
    {
        $joinClauses .= "INNER JOIN zGameParticipants AS Table$i ON Table".($i-1).".GameID = Table".$i.".GameID\n";
    }

    $whereClauses .= "Table$i.ParticipantID = $participantID\n";

    $i++;
}
print $selectClause.$joinClauses.$whereClauses;

This generates this query:

SELECT 
DISTINCT Table0.GameID
FROM zGameParticipants AS Table0
INNER JOIN zGameParticipants AS Table1 ON Table0.GameID = Table1.GameID
INNER JOIN zGameParticipants AS Table2 ON Table1.GameID = Table2.GameID
INNER JOIN zGameParticipants AS Table3 ON Table2.GameID = Table3.GameID
INNER JOIN zGameParticipants AS Table4 ON Table3.GameID = Table4.GameID
INNER JOIN zGameParticipants AS Table5 ON Table4.GameID = Table5.GameID
WHERE
Table0.ParticipantID = 10
Table1.ParticipantID = 11
Table2.ParticipantID = 12
Table3.ParticipantID = 13
Table4.ParticipantID = 14
Table5.ParticipantID = 15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM