MySQL简单加入

时间:2017-04-28 19:15:26

标签: mysql join

我是MySQL的新手,当你有2个外键指向另一个表中的主键时,我试图弄清楚如何最好地处理联接。

基本上,第一个表包含一个gameID(PK)和两个玩家ID(FK)

-----------+------------+------------+
| Game_ID  | Player1_ID | Player2_ID |
+----------+------------+------------+
| 45897    | 569878D    | 458998E    |
| 56984    | 789878S    | 559845S    |
| 56984    | 389845S    | 8998784    |
+----------+------------+------------+ 

第二个表包含玩家详细信息,Player_ID是此处的PK键

-------------+------------+-------------+--------------+
| Player_ID  | First_Name | Second_Name | Expert_Level | 
+------------+------------+-------------+--------------+
| 569878D    | John       | Ryan        | Novice       |
+------------+------------+-------------+--------------+
| 458998E    | James      | Matthew     | Expert       |
+------------+------------+-------------+--------------+
| 789878S    | Nicole     | Martin      | Casual       |
+------------+------------+-------------+--------------+
| 559845S    | Tim        | Farage      | Casual       |
+------------+------------+-------------+--------------+
| 389845S    | Gerard     | Williams    | Novice       |
+------------+------------+-------------+--------------+
| 8998784    | Thompson   | Page        | Novice       |
+------------+------------+-------------+--------------+

我如何加入这两张桌子,作为一个例子,玩家在一个技能水平错误的游戏中。在这种情况下,如下所示,因为一个是新手,第二个是专家

-----------+------------+------------+
| Game_ID  | Player1_ID | Player2_ID |
+----------+------------+------------+
| 45897    | 569878D    | 458998E    |
-----------+------------+------------+

我一直在尝试这样的事情......但似乎无法让它发挥作用。

SELECT Game_Table.Player1_ID AS "Player1", Game_Table.Player2_ID AS "Player2",     Player_Table.Expert_Level
FROM Game_Table, Game_Table 
WHERE Player1.Expert_Level != Player2.Expert_Level

3 个答案:

答案 0 :(得分:1)

好的,你的复制和粘贴技巧以及两个不同问题的混合让我感到害怕....但这就是我认为你之后的事情......

从根本上说,如果你有多个fk返回同一个非复合键的表,那么你需要额外的表别名联接。

SELECT PD.Player_ID as `Person1`
     , PD.Player_ID as `Person2`
     , PT1.Expert_level as `Person1 Expert_Level`
     , PT2.Expert_level as `Person2 Expert Level`
FROM Player_Details PD
LEFT JOIN Person_Table p1
 on P1.PlayerID = PD.Player1_ID
LEFT JOIN Person_Table P2
 on P2.PlayerID = PD.Player2_ID
WHERE P1.Expert_Level <> P2.Expert_level

答案 1 :(得分:1)

试试这个:

SELECT
    a.Game_ID,
    b.Player_ID as `Player_1_ID`,
    b.First_Name as `Player_1_First_Name`,
    b.Last_Name as `Player_1_Last_Name`,
    b.Expert_Level as `Player_1_Expert_Level`,
    c.Player_ID as `Player_2_ID',
    c.First_Name as `Player_2_First_Name`,
    c.Last_Name as `Player_2_Last_Name`,
    c.Expert_Level as `Player_2_Expert_Level`
FROM `games` a
JOIN `players` b
ON a.Player1_ID = b.Player_ID
JOIN `players` c
ON a.Player1_ID = c.Player_ID
WHERE b.Expert_Level <> c.Expert_Level

它应该列出两个玩家的Expert_Level不匹配的所有游戏。

答案 2 :(得分:1)

试试这个:

SELECT 
    g.Player1_ID AS "Player1",
    p1.Expert_Level AS "Player1_Expert_Level",
    g.Player2_ID AS "Player2",     
    p2.Expert_Level AS "Player2_Expert_Level",
FROM Game_Table g
    INNER JOIN Player_Table p1 ON p1.Player_ID = g.Player1_ID
    INNER JOIN Player_Table p2 ON p2.Player_ID = g.Player2_ID 
WHERE p1.Expert_Level <> p2.Expert_Level;