Skip to content
Advertisement

Trying to Full Join two tables in MySql

I was trying to full join to tables as the code below,

SELECT C.cust_name,O.ord_num
FROM customers C
  FULL JOIN orders O
  ON C.cust_code = O.cust_code;

but this code is not working.Is anyone can solve this?

Advertisement

Answer

You can use:

SELECT C.cust_name, O.ord_num
FROM customers C
LEFT JOIN orders O
  ON C.cust_code = O.cust_code
UNION ALL
SELECT null, O.ord_num
FROM orders O
LEFT JOIN customers C
  ON C.cust_code = O.cust_code
WHERE C.cust_code IS NULL;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement