[英]Join 2 tables with all same column headers but one different
How do I join 2 tables that have all of the same column headers but one? 如何联接2个表,这些表具有相同的列标题,但其中一个呢?
The other 3 columns are all the same then one table has the column header "January" and the other table has the column header "February" 其他3列均相同,则一个表的列标题为“ January”,另一表的列标题为“ February”
So I would like to end up with 5 total columns in new table 所以我想最后在新表中添加5列
I would also like all of the data in there even if there are duplicates. 我也想要其中的所有数据,即使有重复也是如此。 Before I used a union all but now I cannot do that because not all my headers are the same.
在我使用并集之前,但现在我都不能这样做,因为并非所有标头都相同。
Thanks! 谢谢!
You can use UNION and rename one of your columns: 您可以使用UNION并重命名其中一列:
SELECT id, name, age, xxx FROM table1
UNION
SELECT id, name, age, yyy AS xxx FROM table2
If you add more information, I can elaborate my answer 如果您添加更多信息,我可以详细说明我的答案
select tb1.col1, tb1.col2, tb1.col3, tb2.col3 from tb1
inner join tb2 on (tb1.col1 = tb2.col1)
Assuming uniqueness when just considering the three columns, joining them 仅考虑三列并将它们连接起来时就假定唯一性
SELECT tab1.col1,
tab1.col2,
tab1.col3,
tab1.january,
tab2.february
FROM tab1,
tab2
WHERE tab1.col1 = tab2.col1
AND tab1.col2 = tab2.col2
AND tab1.col3 = tab2.col3
This assumes all rows in tblA have corresponding ones in tblB and vice versa: 假设tblA中的所有行在tblB中都有对应的行,反之亦然:
SELECT * FROM tblA INNER JOIN tblB USING (col1, col2, col3);
If that is not the case, and depending on the data (if tbl1
. january
can have legitimate null values, this won't work), you could try something like ( didn't check for sure, might need massaged with parenthesis, subselecting, etc... ): 如果不是这种情况,并根据数据(如果
tbl1
。 january
可以有合法的空值,这是不行的),你可以尝试像( 肯定没有检查,可能需要按摩以括号,subselecting等 ):
SELECT *
FROM tbl1
LEFT JOIN tbl2 USING (col1, col2, col3)
UNION ALL
SELECT * FROM tbl2 LEFT JOIN tbl1 USING (col1, col2, col3)
HAVING tbl1.`january` IS NULL
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.