简体   繁体   English

合并具有不同列数的表

[英]Combine tables with different number of columns

I need to UNION two tables that have mostly the same columns, but one has several additional columns.我需要合并两个表,它们的列基本相同,但一个表有几个额外的列。 I'm aware that you can do a SELECT with placeholders for those additional columns, but I'm working with about 200 columns, and the bigger table has about 30 additional columns.我知道您可以使用 SELECT 为这些额外的列添加占位符,但我正在使用大约 200 列,而更大的表有大约 30 列。 This makes it unreasonable to type out every column.这使得键入每一列是不合理的。 Is there a way to union the tables while automatically setting a NULL value for the columns that don't exist in the smaller table?有没有办法在为较小的表中不存在的列自动设置 NULL 值的同时合并表?

In short, no.简而言之,没有。 Unioning result sets together must have the same number / data type of columns. Unioning结果集必须具有相同数量/数据类型的列。 If you wanted to have the remaining sets populate null, the simplest way to do this would be to do something like so-如果您想让剩余的集合填充 null,最简单的方法是执行类似的操作 -

select col1
, col2
, col3
, col4
from tbl1

union all

select null as col1
, null as col2
, null as col3
, null as col4
from tbl2

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

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