多个内部连接mysql

kpbwa7wx  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(383)

我正在写一个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\u 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\u weaponid”
我的表格如下:

我做错什么了?

xlpyo6sf

xlpyo6sf1#

试试这个

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

这个查询背后没有什么神奇的东西。我只是用你最初的成功查询重写它。很可能是因为查询中的表顺序不正确。

9wbgstp7

9wbgstp72#

我做错什么了?
您写道,这种内部连接语法被认为是不好的做法。这是正确的部分-逗号分隔的表连接是交叉连接,而不是内部连接(但在mysql中,它们是synonims-mysql扩展允许任何连接类型子句,而不是另一个只允许交叉连接的DBMS)。坏习惯这个词百分之百正确。
但是你试着使用逗号分隔连接和内部连接的混合-这是一个不错但非常糟糕的做法。
详细说明:
尝试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` 括号内无法访问表副本。
ie3xauqp

ie3xauqp3#

试试这个

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子句来获取三个表连接的输出。

相关问题