【问题标题】:SQL query to union several tables with different number of columnsSQL查询以联合具有不同列数的多个表
【发布时间】:2013-11-19 09:29:15
【问题描述】:

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

【问题讨论】:

  • 为什么同时标记 MySQL 和 SQL Server?您真正使用的是哪个数据库?
  • 我怀疑这不可行。
  • 显示一些示例数据和预期结果。

标签: mysql sql sql-server union


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多