MySQL – Unable to Run FULL OUTER JOIN

join;MySQLmysql-5.6

Here is my query .

 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

here is the error which is happening when i run my query in 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

Best Answer

Full outer join is not supported. If you look at:

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

there is only LEFT and RIGHT outer join. You can however mimic FULL OUTER with a UNION between LEFT and 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 ;

You have to include user_record.id / user_addresses.user_id in your select clause to be able to order by it.