我有 clients
和 partners
表
表 clients
company_id | name
------------------
11 | UA prods.
12 | CI corp.
13 | UAC
表
partners
company_id | name
------------------
11 | UA prods.
12 | CI corp.
20 | BDP Group
30 | UFF
我需要从两个表中获取所有公司名称。当两个表中都存在公司时,则它们的名称,当不存在时,将其留空。
我尝试了左连接、内连接,但它没有给我想要的。
我想要这样的结果
clients | partners
--------------------
UA prods. | UA prods.
CI corp. | CI corp.
UAC |
| BDP Group
| UFF
最佳答案
用这个:
(select clients.name as clients, partners.name as partners from clients
LEFT JOIN partners
ON clients.company_id = partners.company_id)
UNION ALL
(select clients.name as clients, partners.name as partners from partners
LEFT JOIN clients
ON clients.company_id = partners.company_id
WHERE clients.company_id IS NULL)
关于mysql - 从 2 个表中获取连接结果,当值不存在时,用空值填充,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43134468/