多个内部联接mysql

时间:2020-07-28 04:33:30

标签: mysql sql

我正在编写一条sql语句,其中有多个INNER JOIN语句和最后一个WHERE子句。我使用以下查询实现了这一点:

select weapon_name 
  from special_weapons
     , player
     , player_weapons 
 where player.uuid = player_weapons.uuid 
   and player_weapons.weaponid = special_weapons.weaponid;

但是,这种内部联接语法被认为是不好的做法。我尝试多次重写此查询,并不断遇到问题。我尝试了以下所有尝试,但都失败了:

SELECT sp1.WEAPON_NAME 
        FROM SPECIAL_WEAPONS sp1, PLAYER_WEAPONS pw 
    INNER JOIN PLAYER p1 ON 
        pw.UUID = p1.uuid 
    INNER JOIN PLAYER_WEAPONS pw1 ON 
        sp1.weaponid = pw1.weapondid;

错误1054(42S22):“ on子句”中的未知列“ sp1.weaponid”

尝试2:

SELECT WEAPON_NAME 
  FROM SPECIAL_WEAPONS, PLAYER_WEAPONS 
 INNER JOIN PLAYER ON PLAYER_WEAPONS.UUID = PLAYER.UUID 
 INNER JOIN PLAYER_WEAPONS ON SPECIAL_WEAPONS.WEAPONID = 
    PLAYER_WEAPONS.WEAPONID;

错误1066(42000):不是唯一的表/别名:“ PLAYER_WEAPONS”

尝试3:

SELECT WEAPON_NAME 
  FROM SPECIAL_WEAPONS, PLAYER_WEAPONS 
INNER JOIN PLAYER ON PLAYER_WEAPONS.UUID = PLAYER.UUID 
INNER JOIN PLAYER_WEAPONS pw ON SPECIAL_WEAPONS.WEAPONID = pw.WEAPONID;

错误1054(42S22):“ on子句”中的未知列“ SPECIAL_WEAPONS.WEAPONID”

我的表格如下:

enter image description here

我在做什么错了?

3 个答案:

答案 0 :(得分:1)

尝试

SELECT weapon_name
FROM player p
JOIN player_weapons pw ON p.uuid = pw.uuid
JOIN special_weapons sw ON pw.weaponid = sw.weaponid

此查询后没有任何神奇的东西。我只是使用您原来的成功查询来重写它。您很难,可能是因为查询中的表顺序。

答案 1 :(得分:1)

我在做什么错了?

您写道,这种内部联接语法被认为是不道德的做法。部分正确-逗号分隔的表联接是CROSS JOIN,而不是INNER(但在MySQL中,它们是同义词,但MySQL扩展允许任何具有ON子句的JOIN类型子句,而不是仅允许CROSS JOIN的另一个DBMS)。 不良做法一词正确率为100%。

但是,然后您尝试使用逗号分隔的联接和INNER联接的组合-这不错,但是非常糟糕。


详细:

尝试1。

SELECT sp1.WEAPON_NAME 
        FROM SPECIAL_WEAPONS sp1, PLAYER_WEAPONS pw 
    INNER JOIN PLAYER p1 ON 
        pw.UUID = p1.uuid 
    INNER JOIN PLAYER_WEAPONS pw1 ON 
        sp1.weaponid = pw1.weapondid;

表示

SELECT sp1.WEAPON_NAME 
FROM SPECIAL_WEAPONS sp1, (            PLAYER_WEAPONS pw 
                            INNER JOIN PLAYER p1 ON pw.UUID = p1.uuid 
                            INNER JOIN PLAYER_WEAPONS pw1 ON sp1.weaponid = pw1.weapondid 
                          );

sp1别名在括号内不可访问,因为该别名是从别名中定义的。


尝试2。

SELECT WEAPON_NAME 
  FROM SPECIAL_WEAPONS, PLAYER_WEAPONS                    -- first PLAYER_WEAPONS
 INNER JOIN PLAYER ON PLAYER_WEAPONS.UUID = PLAYER.UUID 
 INNER JOIN PLAYER_WEAPONS ON SPECIAL_WEAPONS.WEAPONID =  -- second PLAYER_WEAPONS
    PLAYER_WEAPONS.WEAPONID;

尝试3。

SELECT WEAPON_NAME 
  FROM SPECIAL_WEAPONS, PLAYER_WEAPONS 
INNER JOIN PLAYER ON PLAYER_WEAPONS.UUID = PLAYER.UUID 
INNER JOIN PLAYER_WEAPONS pw ON SPECIAL_WEAPONS.WEAPONID = pw.WEAPONID;

表示

SELECT WEAPON_NAME 
  FROM SPECIAL_WEAPONS, (            PLAYER_WEAPONS 
                          INNER JOIN PLAYER ON PLAYER_WEAPONS.UUID = PLAYER.UUID 
                          INNER JOIN PLAYER_WEAPONS pw ON SPECIAL_WEAPONS.WEAPONID = pw.WEAPONID
                        );

括号中无法访问非别名SPECIAL_WEAPONS表副本。

答案 2 :(得分:0)

尝试一下

SELECT SW.WEAPON_NAME FROM SPECIAL_WEAPONS SW 
INNER JOIN PLAYER_WEAPONS PW ON PW.WEAPONID = SW.WEAPONID 
INNER JOIN PLAYER P ON P.UUID = PW.UUID;

您可以使用WHERE子句获取三个表联接的输出。