MySQL:FULL OUTER JOIN - 如何合并一列?
我有一个关于 MySQL 中的 FULL OUTER JOIN 的问题。我有两个(或更多表):
table1 table2 id value id value2 1 a 1 b 2 c 3 d 3 e 4 f
我已使用此查询来获取我的连接:
SELECT *
FROM table1
LEFT OUTER JOIN table2
ON table1.`id`=table2.`id`
UNION
SELECT *
FROM table1
RIGHT OUTER JOIN table2
ON table1.`id`=table2.`id`
获取:
id value1 id value2 1 a 1 b 2 c NULL NULL 3 e 3 d NULL NULL 4 f
我的问题是我无法同时将两个 id 列折叠成一列来获取此:
id value1 value2 1 a b 2 c NULL 3 e d 4 NULL f
有关如何的任何建议做吗?
I have a question regarding a FULL OUTER JOIN in MySQL. I have two (or more tables):
table1 table2 id value id value2 1 a 1 b 2 c 3 d 3 e 4 f
I have used this query to get my join:
SELECT *
FROM table1
LEFT OUTER JOIN table2
ON table1.`id`=table2.`id`
UNION
SELECT *
FROM table1
RIGHT OUTER JOIN table2
ON table1.`id`=table2.`id`
to get:
id value1 id value2 1 a 1 b 2 c NULL NULL 3 e 3 d NULL NULL 4 f
My problem is that I don't manage to simultaneously collapse the two id columns into one column to get this:
id value1 value2 1 a b 2 c NULL 3 e d 4 NULL f
Any suggestions on how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用:
UNION 运算符删除行/记录重复项,因此您必须适当地定义/列出列。
脚本:
编辑:上面的固定行
Use:
The
UNION
operator removes row/record duplicates, so you have to define/list the columns appropriately.Scripts:
Edit: Fixed line above
对于我认为您正在尝试做的事情,我建议使用 FULL OUTER JOIN 代替:
For what I think you are trying to do, I would suggest using a FULL OUTER JOIN instead: