繁体   English   中英

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

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

我有几张表,前三行相同,其余各行不同(在数字上也一样)。 我想将所有表合并成一个表,前三列保持不变,其余表连接在一起成为一个名为“描述”的列。 现在,我能够手动执行此操作,但是我有很多表。 可以使用带有变量的while循环吗? 还是可行?

听起来您想将表连接在一起。 在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