将大连接查询与重复查询相结合

时间:2017-11-24 14:50:51

标签: mysql sql

我正在努力将这两个sql查询结合起来。

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 tournaments ON earnings.tournament = tournaments.tournament_id
  where position = 1
ORDER BY players.player_id;
SELECT tournament, earnings.player, prize_money, position 
FROM earnings 
join (
    SELECT player FROM earnings 
    where position = 1 group by player having count(player) > 3) 
as DupPlayer
on earnings.player=DupPlayer.player
where position = 1;

所以我尝试了一个工会。

(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 tournaments ON earnings.tournament = tournaments.tournament_id
  where position = 1
ORDER BY players.player_id
)
UNION
(
SELECT tournament, earnings.player, prize_money, position 
FROM earnings 
join (
    SELECT player FROM earnings 
    where position = 1 group by player having count(player) > 3) 
as DupPlayer
on earnings.player=DupPlayer.player
where position = 1
);

然后我收到错误消息“使用的SELECT语句具有不同的列数”。我该怎么办呢?有没有比工会更好的方法?我真的不想改变我的选择语句,因为我想看到所有这些列。

1 个答案:

答案 0 :(得分:2)

合并以下查询:

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) > 3) 
        as DupPlayer on earnings.player=DupPlayer.player
  JOIN tournaments ON earnings.tournament = tournaments.tournament_id
  where position = 1
ORDER BY players.player_id;