简体   繁体   English

mysql选择distinct在多行中的哪个位置

[英]mysql select distinct where in multiple rows

Consider a table, zGameParticipants with these two columns and values 考虑一个包含这两列和值的表,zGameParticipants

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 我想选择具有ParticipantID 10和11的独特GameID。出于显而易见的原因,我不能这样做

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

where $ParticipantIDs is an array with ParticipantIDs. 其中$ParticipantIDs是具有ParticipantID的数组。

There can be n ParticipantIDs, so adding n ParticipantID to an array and bind as parameters may work somehow. 可以有n个ParticipantID,因此将n ParticipantID添加到数组并绑定为参数可能会以某种方式工作。 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 然后只需添加GROUP BY GameID

this is assuming that $ParticipantIDs is a comma separated list like this 这假设$ ParticipantIDs是这样的逗号分隔列表

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 如果你不知道如何使它成为逗号分隔列表,那么在php中使用implode就像这样

$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 我建议你研究一下参数化查询,然后将参数绑定到IN()语句中

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 添加到@John Ruddell

Example

Input params : ParticipantID in range (13,14,15) 输入参数 :参与者ID在范围内(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. 我添加了HAVING=3 ,因为它保证了GameID有13 AND 14 AND 15。

I believe you are looking for the GameIDs that have ALL the participants listed in the array. 我相信您正在寻找具有阵列中列出的所有参与者的GameID。 (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) (这就是为什么你不能使用你发布的SQL查询,因为这将选择具有任何参与者的GameID)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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