【问题标题】:how to return all the columns not just the updated columns when update in postgresql?在postgresql中更新时如何返回所有列而不仅仅是更新的列?
【发布时间】:2021-03-06 02:40:59
【问题描述】:

假设我有这个更新声明

UPDATE person set age = 20,location = 'us' WHERE id = 1234

基于 UPDATE 上的 postgresql 文档,它只能返回更新的字段。 但无论是否更新,我都想获取所有字段

RETURNING *

没用

【问题讨论】:

    标签: postgresql sql-update


    【解决方案1】:

    returning * 返回已更新行的所有列。

    Quote from the manual

    如果 UPDATE 命令包含 RETURNING 子句,则结果将类似于包含 RETURNING 列表中定义的列和值的 SELECT 语句的结果

    所以returning * 的工作方式与select * 非常相似

    postgres=# create table foo (id int, c1 int, c2 int, c3 int, c4 int);
    CREATE TABLE
    postgres=# insert into foo values (1, 2, 3, 4, 5);
    INSERT 0 1
    postgres=# update foo set c4 = 42 where id = 1 returning *;
     id | c1 | c2 | c3 | c4
    ----+----+----+----+----
      1 |  2 |  3 |  4 | 42
    (1 row)
    
    UPDATE 1
    postgres=#
    

    Online example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      相关资源
      最近更新 更多