【问题标题】:Update a column of a table with data from another table if columns match PostgreSQL如果列与 PostgreSQL 匹配,则使用另一个表中的数据更新表的列
【发布时间】:2018-11-14 04:01:58
【问题描述】:

如果列中的值与另一个表中的列中的另一个值匹配,我想将多个值添加到列中。

例如

表1 列 1.1 第1.2列

表2 列2.1 第2.2列

如果 column2.2 = column1.1,则将 column1.2 更新为第 2.1 列

应该能够将多个值写入第 2.1 列

这是我所拥有的,但它不起作用。

SET column1.2 = table2.column2.1 从表 2 WHERE table1.column1.1 = table2.column2.2

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。
  • @GordonLinoff,添加图片看看是否有帮助

标签: sql postgresql sql-update


【解决方案1】:

您似乎想要另一个表中的update。语法如下:

update table1
    set column1 = table2.column1 
    from table2 
    where table1.column2 = table2.column2;

我无法跟随您的问题中列名的变化,但这是从另一个表更新一个表中的列的结构。

【讨论】:

    【解决方案2】:

    应该这样做 -

    update table1
        set column1.2 = table1.column1.2 || ' ' || table2.column2.1 
        from table2 
        where table1.column1.1 = table2.column2.2;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 2018-07-20
      • 2020-10-08
      • 2020-05-18
      • 2022-07-29
      相关资源
      最近更新 更多