[英]SQL Merge tables with two different columns
I have two tables that look like this:我有两个看起来像这样的表:
table1:表格1:
table2:表2:
and I'd like to merge them, to create a table with a header like this:我想合并它们,以创建一个带有如下标题的表:
variablenname | mean | stddev | ms_form | dependent | fvalue | probf
The first nine rows should be the first table with empty values in the last 3 columns followed by the three rows from the second table with empty values in the first 4 columns.前 9 行应该是第一个表,最后 3 列是空值,然后是第二个表中的三行,前 4 列是空值。 I'm sorry I can't post pictures or a third link, but my reputation is too low.很抱歉我不能发布图片或第三个链接,但我的声誉太低了。 I hope it's understandable.我希望这是可以理解的。
I tried to create a new table and select everything from both with union
but that didn't work.我尝试创建一个新表并使用union
从两者中选择所有内容,但这没有用。 Can anybody help me?有谁能够帮助我?
Thanks!谢谢!
You can use ALTER to add the last 3 columns to table 1. From there, INSERT table 2 onto table 1. So something like this.您可以使用 ALTER 将最后 3 列添加到表 1。从那里,将表 2 插入到表 1 中。就像这样。
ALTER table1 ADD dependent varchar(16)
ALTER table1 ADD favalue varchar(16)
ALTER table1 ADD probf varchar(16)
INSERT INTO table1(dependent, fvalue, probf)
SELECT * FROM table2
You can use a combination of joins and unions您可以使用连接和联合的组合
LEFT JOIN
the two tables in first query on fields that won't have a match then UNION
the second query with a RIGHT JOIN
of the same tables. LEFT JOIN
在第一个查询中对不匹配的字段的两个表然后UNION
第二个查询与相同表的RIGHT JOIN
。
The LEFT JOIN
will only show results from the first table, then the RIGHT JOIN
will only show results from the second LEFT JOIN
只会显示第一个表的结果,然后RIGHT JOIN
只会显示第二个表的结果
Something Like:就像是:
SELECT * FROM table1 as t1
LEFT JOIN table1 as t2 on t1.variablenname = t2.fValue
UNION
SELECT * FROM table1 as t1
RIGHT JOIN table1 as t2 on t1.variablenname = t2.fValue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.