为sql查询添加限制

时间:2017-11-24 17:04:42

标签: mysql sql

我有这个SQL查询:

SELECT players.tag, players.game_race, tournaments.region
FROM players
JOIN earnings ON players.player_id = earnings.player 
JOIN (SELECT player FROM earnings 
    where position = 1 group by player having count(player) > 2) 
    as DupPlayer on earnings.player=DupPlayer.player
JOIN tournaments ON earnings.tournament = tournaments.tournament_id
where earnings.position = 1 and tournaments.region is not NULL
ORDER BY players.player_id;

我想删除所有没有AMEUKr区域标记的记录。就像你看creator标签一样,它只有Kr区域,所以我想摆脱它。与PartinG相同,因为它只有Am区域。

+-------------+-----------+--------+
| tag         | game_race | region |
+-------------+-----------+--------+
| Leenock     | Z         | AM     |
| Leenock     | Z         | EU     |
| Leenock     | Z         | AM     |
| Leenock     | Z         | AM     |
| Leenock     | Z         | EU     |
| Leenock     | Z         | KR     |
| Creator     | P         | KR     |
| Creator     | P         | KR     |
| Life        | Z         | AM     |
| Life        | Z         | EU     |
| Life        | Z         | KR     |
| Life        | Z         | KR     |
| Life        | Z         | AM     |
| Life        | Z         | KR     |
| Life        | Z         | KR     |
| Life        | Z         | EU     |
| Life        | Z         | EU     |
| DongRaeGu   | Z         | KR     |
| DongRaeGu   | Z         | KR     |
| DongRaeGu   | Z         | KR     |
| DongRaeGu   | Z         | KR     |
| DongRaeGu   | Z         | EU     |
| DongRaeGu   | Z         | AM     |
| DongRaeGu   | Z         | AM     |
| DongRaeGu   | Z         | AM     |
| PartinG     | P         | AM     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | EU     |
| TaeJa       | T         | AM     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | EU     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | EU     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | EU     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | EU     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | KR     |
| TaeJa       | T         | KR     |

我认为这会起作用,但它只是造成一堆重复。

SELECT players.player_id, players.tag, players.game_race,
   earnings.tournament, earnings.player, earnings.position,
   tournaments.tournament_id, tournaments.region
FROM players
JOIN earnings ON players.player_id = earnings.player 
JOIN (SELECT player FROM earnings 
    where position = 1 group by player having count(player) > 2) 
    as DupPlayer on earnings.player=DupPlayer.player
JOIN (SELECT region FROM tournaments 
    group by region having count(region) > 2) 
    as DupPlayer1 on region=DupPlayer1.region
JOIN tournaments ON earnings.tournament = tournaments.tournament_id
where earnings.position = 1 and tournaments.region is not NULL
ORDER BY players.player_id;

尝试这个建议。这似乎不起作用。它让Parting落后,而SjoW则拥有所有欧盟。

> SELECT players.tag, players.game_race, tournaments.region
    -> FROM players
    -> JOIN earnings ON players.player_id = earnings.player
    -> JOIN (SELECT player FROM earnings
    ->     where position = 1 group by player having count(player) > 2)
    ->     as DupPlayer on earnings.player=DupPlayer.player
    -> JOIN tournaments ON earnings.tournament = tournaments.tournament_id
    ->     and tournaments.region in ('AM', 'EU', 'KR')
    -> where earnings.position = 1 and tournaments.region is not NULL
    -> and players.tag in (
    ->   SELECT players.tag
    ->   FROM players
    ->   JOIN earnings ON players.player_id = earnings.player
    ->   JOIN (SELECT player FROM earnings
    ->       where position = 1 group by player having count(player) > 2)
    ->       as DupPlayer on earnings.player=DupPlayer.player
    ->   JOIN tournaments ON earnings.tournament = tournaments.tournament_id
    ->       and tournaments.region in ('AM', 'EU', 'KR')
    ->       group by players.tag
    ->       having count(distinct tournaments.region) = 3
    -> )
    -> ORDER BY players.player_id;
+------------+-----------+--------+
| tag        | game_race | region |
+------------+-----------+--------+
| Leenock    | Z         | AM     |
| Leenock    | Z         | AM     |
| Leenock    | Z         | AM     |
| Leenock    | Z         | EU     |
| Leenock    | Z         | EU     |
| Leenock    | Z         | KR     |
| Life       | Z         | AM     |
| Life       | Z         | AM     |
| Life       | Z         | EU     |
| Life       | Z         | EU     |
| Life       | Z         | KR     |
| Life       | Z         | KR     |
| Life       | Z         | KR     |
| Life       | Z         | KR     |
| Life       | Z         | EU     |
| DongRaeGu  | Z         | KR     |
| DongRaeGu  | Z         | KR     |
| DongRaeGu  | Z         | AM     |
| DongRaeGu  | Z         | AM     |
| DongRaeGu  | Z         | AM     |
| DongRaeGu  | Z         | KR     |
| DongRaeGu  | Z         | EU     |
| DongRaeGu  | Z         | KR     |
| PartinG    | P         | AM     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | AM     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | EU     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | EU     |
| TaeJa      | T         | EU     |
| TaeJa      | T         | EU     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | EU     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | KR     |
| TaeJa      | T         | KR     |
| viOLet     | Z         | AM     |
| viOLet     | Z         | AM     |
| viOLet     | Z         | EU     |
| HerO       | P         | AM     |
| HerO       | P         | AM     |
| HerO       | P         | EU     |
| HerO       | P         | EU     |
| HerO       | P         | AM     |
| SjoW       | T         | EU     |
| SjoW       | T         | EU     |
| SjoW       | T         | EU     |
| SjoW       | T         | EU     |
| SjoW       | T         | EU     |

2 个答案:

答案 0 :(得分:0)

如果只需要所有3个区域'AM','EU','KR'的结果 你应该过滤这个结果并检查标签taht是否都使用带有计数的子查询(distinct region)= 3

SELECT players.tag, players.game_race, tournaments.region
FROM players
JOIN earnings ON players.player_id = earnings.player 
JOIN (SELECT player FROM earnings 
    where position = 1 group by player having count(player) > 2) 
    as DupPlayer on earnings.player=DupPlayer.player
JOIN tournaments ON earnings.tournament = tournaments.tournament_id 
    and tournaments.region in ('AM', 'EU', 'KR')
where earnings.position = 1 and tournaments.region is not NULL
and players.tag in ( 
  SELECT players.tag
  FROM players
  JOIN earnings ON players.player_id = earnings.player 
  JOIN (SELECT player FROM earnings 
      where position = 1 group by player having count(player) > 2) 
      as DupPlayer on earnings.player=DupPlayer.player
  JOIN tournaments ON earnings.tournament = tournaments.tournament_id 
      and tournaments.region in ('AM', 'EU', 'KR')
      group by players.tag
      having count(distinct tournaments.region) = 3
)
ORDER BY players.player_i

答案 1 :(得分:0)

这看起来像你添加的部分:

JOIN (SELECT region FROM tournaments 
    group by region having count(region) > 2)

你有正确的总体想法,但错误的细节。更改子查询以仅选择所需的3个区域中的玩家。