【问题标题】:Get combined data about two players from a table with game matches从具有游戏匹配的表中获取有关两个玩家的组合数据
【发布时间】:2023-01-27 00:36:29
【问题描述】:

我有一个包含匹配项的表,每行包含两个外键,每个外键都引用一个单独的玩家表中的玩家。

我怎样才能选择数据,以便它在一行中返回两个玩家的完整数据,比如他们的名字和姓氏?

SELECT * FROM Matches
JOIN Players ON Matches.Player1_ID=Players.Player_ID
JOIN Players ON Matches.Player2_ID=Players.Player_ID

这行不通

【问题讨论】:

  • 使用不同的表别名,例如JOIN Players p1 ON Matches.Player1_ID=p1.Player_ID

标签: mysql sql


【解决方案1】:

您可以使用设置表的别名作为, 然后加入.

SELECT Matches.*, Ps1.*, Ps2.* FROM Matches
JOIN Players as Ps1 ON Matches.Player1_ID=Ps1.Player_ID
JOIN Players as Ps2 ON Matches.Player2_ID=Ps2.Player_ID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2011-03-12
    • 2014-06-05
    相关资源
    最近更新 更多