gpt4 book ai didi

mysql - 合并 MySQL 中的 2 个表并自动填充缺失的列

转载 作者:行者123 更新时间:2023-11-29 08:06:19 25 4
gpt4 key购买 nike

我有一个小问题,我想合并 2 个表(并非所有列都相同),并在列不同的地方放置 NULL...

很难用语言来解释这一点,所以这是我想要做的一个例子:

如果我有这张 table :

+------+------+------+------+
| col1 | col2 | col3 | type |
+------+------+------+------+
| 1 | NULL | 1 | A |
| NULL | 1 | NULL | A |
+------+------+------+------+

这张表:

+------+------+------+------+
| col2 | col3 | col4 | type |
+------+------+------+------+
| 1 | NULL | 1 | B |
| NULL | 1 | NULL | B |
+------+------+------+------+

我想创建一个像这样的表:

+------+------+------+------+------+
| col1 | col2 | col3 | col4 | type |
+------+------+------+------+------+
| 1 | NULL | 1 | NULL | A |
| NULL | 1 | NULL | NULL | A |
| NULL | 1 | NULL | 1 | B |
| NULL | NULL | 1 | NULL | B |
+------+------+------+------+------+

这可能吗? ^.^

编辑:在真实的表格中,第一个表格大约有 40 列,第二个表格大约有 80 列

最佳答案

当然,只需将记录集与每个表中不存在的列中的空值合并即可:

insert into YourMergeTable
select col1,col2,col3,null col4,type from YourFirstTable
union all
select null col1,col2,col3,col4,type from YourSecondTable

http://www.sqlfiddle.com/#!2/16110/1

关于mysql - 合并 MySQL 中的 2 个表并自动填充缺失的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22675680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com