简体   繁体   English

尝试在 MySql 中完全连接两个表

[英]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?但是这段代码不起作用。有人可以解决这个问题吗?

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;

FULL OUTER JOIN result without FULL OUTER JOIN support: FULL OUTER JOIN 结果没有 FULL OUTER JOIN 支持:

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)

UNION ALL & LEFT JOIN will be equivalent : UNION ALL & LEFT JOIN将是等效的:

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;

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没有这样的 Join FULL JOIN ,正确的是FULL OUTER JOIN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM