简体   繁体   English

多个内连接 mysql

[英]Multiple inner joins mysql

I am writing an sql statement in which I have multiple INNER JOIN statements with a final WHERE clause.我正在编写一个 sql 语句,其中我有多个带有最终WHERE子句的INNER JOIN语句。 I have achieved this using the following query:我使用以下查询实现了这一点:

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

However, this inner join syntax is considered bad practice.但是,这种内连接语法被认为是不好的做法。 I have tried rewriting this query numerous times and continually run into a gambit of problems.我曾多次尝试重写此查询并不断遇到问题。 I attempted the following, all of which failed:我尝试了以下方法,但都失败了:

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;

ERROR 1054 (42S22): Unknown column 'sp1.weaponid' in 'on clause错误 1054 (42S22):“on 子句”中的未知列“sp1.weaponid”

Attempt 2:尝试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;

ERROR 1066 (42000): Not unique table/alias: 'PLAYER_WEAPONS'错误 1066 (42000): 不是唯一的表/别名: 'PLAYER_WEAPONS'

Attempt 3:尝试 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;

ERROR 1054 (42S22): Unknown column 'SPECIAL_WEAPONS.WEAPONID' in 'on clause'错误 1054 (42S22):“on 子句”中的未知列“SPECIAL_WEAPONS.WEAPONID”

My tables look as follows:我的表格如下所示:

在此处输入图像描述

What am I doing wrong?我究竟做错了什么?

try this尝试这个

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

there is no magic stuff behind this query.这个查询背后没有神奇的东西。 I just rewrite it using your original successful query.我只是用你原来的成功查询重写它。 you had a hard time probably because the table order in the query.您可能遇到了困难,因为查询中的表顺序。

What am I doing wrong?我究竟做错了什么?

You write that this inner join syntax is considered bad practice.您写道,这种内连接语法被认为是不好的做法。 This is true partially - comma-separated tables join is CROSS JOIN, not INNER (but in MySQL they're synonims - MySQL extension allows any JOIN type clause without ON clause rather than another DBMSs which allows this for CROSS JOIN only).这部分是正确的 - 逗号分隔的表连接是 CROSS JOIN,而不是 INNER(但在 MySQL 中它们是同义词 - MySQL 扩展允许任何不带 ON 子句的 JOIN 类型子句,而不是另一个仅允许 CROSS JOIN 的 DBMS)。 The words bad practice are correct 100%.坏习惯这句话是 100% 正确的。

But then you try to use the mix of comma-separated join and INNER join - this is not bad but extremely bad practice.但是随后您尝试混合使用逗号分隔连接和 INNER 连接 - 这不是坏事,但却是非常糟糕的做法。


And detailed:并且详细:

Attempt1.尝试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;

means方法

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 alias is not accessible within the parenthesis because this alias is defined out of them. sp1别名不能在括号内访问,因为此别名是在括号中定义的。


Attempt 2.尝试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;

Attempt 3.尝试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;

means方法

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
                        );

Non-aliased SPECIAL_WEAPONS table copy is not accessible within the parenthesis.括号内无法访问非别名的SPECIAL_WEAPONS表副本。

Try this尝试这个

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;

you can use the WHERE clause to get output of three table join.您可以使用 WHERE 子句获取三表连接的 output。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM