为什么MySQL在FULL OUTER JOIN上报告语法错误?

yduiuuwa  于 2023-05-05  发布在  Mysql
关注(0)|答案(5)|浏览(229)
SELECT airline, airports.icao_code, continent, country, province, city, website 

FROM airlines 
FULL OUTER JOIN airports ON airlines.iaco_code = airports.iaco_code
FULL OUTER JOIN cities ON airports.city_id = cities.city_id
FULL OUTER JOIN provinces ON cities.province_id = provinces.province_id
FULL OUTER JOIN countries ON cities.country_id = countries.country_id
FULL OUTER JOIN continents ON countries.continent_id = continents.continent_id

上面说
SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以获得正确的语法,以在第4行使用'outer join airports on airlines.iaco_code = airports.iaco_code full outer join'附近
我觉得语法是对的。我从来没有做过很多连接之前,但我需要这些列在一个表,这是交叉引用的各种id的。

20jt8wwn

20jt8wwn1#

MySQL中没有FULL OUTER JOIN。参见7.2.12. Outer Join Simplification12.2.8.1. JOIN Syntax
您可以使用UNION(从MySQL 4.0.0开始)模拟FULL OUTER JOIN
其中两个表t1、t2:

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id

有三个表t1、t2、t3:

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
RIGHT JOIN t3 ON t2.id = t3.id
neekobn8

neekobn82#

克莱图斯的回答不太对。UNION将删除FULL OUTER JOIN可能包含的重复记录。如果您需要使用类似以下内容的副本:

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id
LEFT JOIN t4 ON t3.id = t4.id
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t2.id = t3.id
LEFT JOIN t4 ON t3.id = t4.id
WHERE t1.id IS NULL
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
RIGHT JOIN t3 ON t2.id = t3.id
LEFT JOIN t4 ON t3.id = t4.id
WHERE t2.id IS NULL
UNION ALL
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
RIGHT JOIN t3 ON t2.id = t3.id
RIGHT JOIN t4 ON t3.id = t4.id
WHERE t3.id IS NULL;
yhived7q

yhived7q3#

我刚刚为此做了一个小把戏:

(select 1 from DUAL) d
LEFT OUTER JOIN t1 ON t1.id = t2.id
LEFT OUTER JOIN t2 ON t1.id = t2.id

关键是,来自dual的查询是一个固定点,mysql可以将另外两个表外连接到该固定点

3xiyfsfu

3xiyfsfu4#

只需补充一下需要FULL OUTER JOIN三个表t1、t2、t3的情况。你可以依次做t1,t2,t3,左连接剩下的两个表,然后合并。

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 ON t1.id = t3.id
UNION
SELECT * FROM t2
LEFT JOIN t1 ON t2.id = t1.id
LEFT JOIN t3 ON t2.id = t3.id
UNION
SELECT * FROM t3
LEFT JOIN t1 ON t3.id = t1.id
LEFT JOIN t2 ON t3.id = t2.id
6tr1vspr

6tr1vspr5#

只是为了补充这里提供的答案,我想分享这个连接4个表的例子,在所有SELECT语句上使用LEFT JOIN,还有一个更形象的例子。

SELECT doctor.name, professor.name, singer.name, actor.name
FROM doctor
LEFT JOIN professor ON professor.id = doctor.id
LEFT JOIN singer ON singer.id = doctor.id
LEFT JOIN actor ON actor.id = doctor.id
UNION
SELECT doctor.name, professor.name, singer.name, actor.name
FROM professor
LEFT JOIN doctor ON doctor.id = professor.id
LEFT JOIN singer ON singer.id = professor.id
LEFT JOIN actor ON actor.id = professor.id
UNION
SELECT doctor.name, professor.name, singer.name, actor.name
FROM singer
LEFT JOIN doctor ON doctor.id = singer.id
LEFT JOIN professor ON professor.id = singer.id
LEFT JOIN actor ON actor.id = singer.id
UNION
SELECT doctor.name, professor.name, singer.name, actor.name
FROM actor
LEFT JOIN doctor ON doctor.id = actor.id
LEFT JOIN professor ON professor.id = actor.id
LEFT JOIN singer ON singer.id = actor.id;

相关问题