Mysql

無法執行 FULL OUTER JOIN

  • June 25, 2018

這是我的查詢。

SELECT user_record.password,user_addresses.street 
FROM `user_record` 
FULL OUTER JOIN `user_addresses` 
    ON user_record.id=user_addresses.user_id 
ORDER by user_record.id

這是我在 Phpmyadmin 中執行查詢時發生的錯誤。

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'OUTER JOIN `user_addresses` ON user_record.id=user_addresses.user_id ORDER by us' at line 1

Full outer join不支持。如果你看:

https://dev.mysql.com/doc/refman/5.7/en/join.html

只有LEFTRIGHT外連接。FULL OUTER但是,您可以使用UNIONbetweenLEFT和進行模仿RIGHT

SELECT user_record.id AS user_id, 
      user_record.password, user_addresses.street 
FROM `user_record` 
LEFT JOIN `user_addresses` 
   ON user_record.id = user_addresses.user_id 

UNION

SELECT user_addresses.user_id, 
      user_record.password, user_addresses.street 
FROM `user_record` 
RIGHT JOIN `user_addresses` 
   ON user_record.id = user_addresses.user_id 

ORDER BY user_id ;

您必須包含user_record.id / user_addresses.user_id在您的 select 子句中才能通過它進行排序。

引用自:https://dba.stackexchange.com/questions/210510