简体   繁体   English

连接具有相同列号和名称的两个表

[英]Concatenating two tables with same column number and name

I have two tables: 我有两个表:

TABLE_A TABLE_A

head1 head2
foo   case1
bar   case2

and TABLE_B 和TABLE_B

head1 head2
hux   case1
pix   case2
ilf   case1

What I want to do is to create this table: 我想要做的是创建此表:

head1 head2
foo case1
bar case2
hux case1
pix case2
ilf case3

Namely just combine them, without removing any redundancy. 即仅将它们组合在一起,而不会删除任何冗余。

What's the right command to do it? 正确的命令是什么? I tried this but it didn't give the the desired result. 我试过了,但没有得到想要的结果。

CREATE TABLE TABLE_AB(head1 text, head2 text)
(SELECT * FROM TABLE_A)
UNION ALL
(SELECT * FROM TABLE_B)

You may change the columns names to the desired ones. 您可以将列名称更改为所需的名称。

CREATE TABLE TAB1 (ID number, NAME VARCHAR2(10));
CREATE TABLE TAB2 (ID number, NAME VARCHAR2(10));

INSERT INTO TAB1 VALUES (1, 'AAA');
INSERT INTO TAB1 VALUES (2, 'BBB');
INSERT INTO TAB1 VALUES (3, 'CCC');

INSERT INTO TAB2 VALUES (1, 'EEE');
INSERT INTO TAB2 VALUES (4, 'FFF');

CREATE TABLE TAB AS ((SELECT * FROM TAB1) UNION ALL (SELECT * FROM TAB2));

You can omit the parenthesis around the union parts. 您可以省略联合部分周围的括号。 Also, for create table ... select , you don't have the specify the column definitons. 同样,对于create table ... select ,您没有指定列definitons。

create table Table3
select  *
from    Table1
union all
select  *
from    Table2

Example at SQL Fiddle. SQL Fiddle中的示例。

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

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