【问题标题】:merging multiple Columns from table1 into one column in table2将table1中的多个列合并为table2中的一列
【发布时间】:2016-02-19 04:00:47
【问题描述】:

我想将 Table1 中的多个现有列合并或插入到 Table2 中的一列中。我在表 1 中有客户的地址,但它存储在单个实体中(街道地址、公寓、城市、州等)。我想做的是将所有这些实体合并到一列中,作为每个客户的一个地址。我试过了:

insert into table2(newAddress) select street_address, city, state, country from table1

但是,我收到此错误“列计数与第 1 行的值计数不匹配”。谢谢你的帮助

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    如果您尝试为单个字段插入值,SQL 期望您只返回 1 个字段。在您的情况下,您将返回 4 个字段:street_address, city, state, country

    我不确定您的最终实现应该是什么,但您可以尝试将 4 个字段合并为一个:

    insert into table2(newAddress) select street_address + ' ' + city + ' ' + state + ' ' + country AS newAddress from table1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      相关资源
      最近更新 更多