0

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?

7
  • Why would you need a full join for this? Isn't each order related to one and and only one customer? Commented Jan 20, 2020 at 10:40
  • use full outer join Commented Jan 20, 2020 at 10:42
  • @GMB no there are 33 customers in the customers table Commented Jan 20, 2020 at 10:42
  • @Shadiqur same syntax error is appeared Commented Jan 20, 2020 at 10:43
  • 1
    Show us some sample table data and the expected result (minimal reproducible example). Commented Jan 20, 2020 at 10:44

4 Answers 4

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

FULL OUTER JOIN result without FULL OUTER JOIN support:

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
WHERE NOT EXISTS (select 1 from customers C where C.cust_code = O.cust_code)

Comments

0

UNION ALL & LEFT JOIN will be equivalent :

SELECT C.cust_name, O.ord_num
FROM customers C LEFT JOIN
     orders O
     ON C.cust_code = O.cust_code
UNION ALL -- Use UNION if you don't want duplicate
SELECT C.cust_name, O.ord_num
FROM orders O LEFT JOIN
     customers C
     ON C.cust_code = O.cust_code;

2 Comments

Will return duplicates for the matching rows.
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.cust_code' at line 8 this error was shown up
0

You are using Full Join in your Query, Correct Query will be:

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

There is no such Join FULL JOIN, Correct is FULL OUTER JOIN

1 Comment

FULL JOIN is the same as FULL OUTER JOIN, since the OUTER keyword is optional. However, MySQL doesn't support it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.