如何合并以下多个选择查询的结果,以便可以在一个合并的视图中获得有关所有战斗的信息。要获得完整的架构,请查看here,因为stackoverflow限制了我可以编写多少代码来描述问题。
|battle.id |Attackers | Defender | Winner |
--------------------------------------------------------------------
| 1 |'rishabhchauhan'|'vaibhavchauhan' |'vaibhavchauhan'|
| 2 |'abhishekgaur' |'vaibhavchauhan' |'vaibhavchauhan'|
| 3 |'AnkitSharma' |'vaibhavchauhan' |'vaibhavchauhan'|
....
查询以查找攻击者姓名:
select CONCAT(player.firstname, player.lastname) as Attacker, Battles.battle_id
from ((player inner
join Attacker_Battles on Attacker_Battles.player_id = player.player_id )
inner join Battles on Battles.battle_id = Attacker_Battles.battle_id);
查询以找到防御者姓名:
select CONCAT(player.firstname, player.lastname) as Defenders, Battles.battle_id
from ((player inner
join Defender_Battles on Defender_Battles.player_id = player.player_id )
inner join Battles on Battles.battle_id = Defender_Battles.battle_id);
查询以查找获奖者姓名:
select CONCAT(player.firstname, player.lastname) as Winners, Battles.battle_id
from ((player inner
join Winner_battles on Winner_battles.player_id = player.player_id )
inner join Battles on Battles.battle_id = Winner_battles.battle_id);
答案 0 :(得分:0)
要将所有查询合并为一个查询,您只需要将JOIN
表Battles
到每个Attacker_Battles
等表中,然后将每个查询都{{1 }}表以获取适当的名称。这样的事情应该起作用:
player
输出(用于您的pastebin数据)
SELECT CONCAT(pa.firstname, pa.lastname) AS Attacker,
CONCAT(pd.firstname, pd.lastname) AS Defender,
CONCAT(pw.firstname, pw.lastname) AS Winner,
b.battle_id As Battle
FROM Battles b
JOIN Attacker_Battles ab ON ab.battle_id = b.battle_id
JOIN player pa ON pa.player_id = ab.player_id
JOIN Defender_Battles db ON db.battle_id = b.battle_id
JOIN player pd ON pd.player_id = db.player_id
JOIN Winner_Battles wb ON wb.battle_id = b.battle_id
JOIN player pw ON pw.player_id = wb.player_id
查询也可以如下编写(也许更直观)。如果给定的Attacker Defender Winner Battle
rishabhchauhan vaibhavchauhan vaibhavchauhan 1
abhishekgaur pawanyadav abhishekgaur 2
abhishekgaur pawanyadav abhishekgaur 3
AnkitSharma JayantVerma AnkitSharma 4
vaibhavchauhan JayantVerma JayantVerma 5
rishabhchauhan abhishekgaur rishabhchauhan 6
vaibhavchauhan AnkitSharma vaibhavchauhan 8
可能没有Battle
,Attacker
和Defender
,则此查询中的顶级Winner
可以替换为JOIN
:
LEFT JOIN