简体   繁体   English

SQL查询以合并具有不同列数的多个表

[英]SQL query to union several tables with different number of columns

I have several tables with the first three rows identical and the rest different (in numbers as well). 我有几张表,前三行相同,其余各行不同(在数字上也一样)。 I want to union all tables into one, with first three columns as they are and the rest concatenated together into a single column named as "description". 我想将所有表合并成一个表,前三列保持不变,其余表连接在一起成为一个名为“描述”的列。 Now, I was able to do this manually but i have lots of tables. 现在,我能够手动执行此操作,但是我有很多表。 can a while loop be used, with variables? 可以使用带有变量的while循环吗? or is it doable? 还是可行?

It sounds like you want to join the tables together. 听起来您想将表连接在一起。 In MySQL, this would look like: 在MySQL中,这看起来像:

select t1.col1, t1.col2, t1.col3,
       concat(t1.col4, t2.col4, t3.col4) as description
from t1 left outer join
     t2
     on t1.col1 = t2.col1 and t1.col2 = t2.col2 and t1.col3 = t2.col3 left outer join
     t3
     on t1.col1 = t3.col1 and t1.col2 = t3.col2 and t1.col3 = t3.col3

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

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