为什么MySQL在FULL OUTER JOIN上报告语法错误?
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行的“airlines.iaco_code = airports.iaco_code full outer join”的“外部join
airports
”附近使用正确的语法
语法看起来很正确。 我以前从来没有做过很多的连接,但是我需要一个由各种id交叉引用的表中的列。
MySQL中没有FULL OUTER JOIN
。 见7.2.12。 外部简化和12.2.8.1。 JOIN语法 :
您可以使用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
克莱图斯的答案并不完全正确。 UNION
将删除FULL OUTER JOIN
将包含的重复logging。 如果你需要使用类似的东西重复:
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;
我刚刚为此做了一个诡计:
(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可以外部连接另外两个表