【问题标题】:There is a column named ... it cannot be referenced from this part of the query sub query有一个名为...的列不能从这部分查询子查询中引用
【发布时间】:2018-11-17 05:44:36
【问题描述】:
 WITH upt as (
 UPDATE backend."orders" SET "statusId" = 5
 WHERE "userId" IN (177962,88265) and "statusId" IN (0,1,2,3,4) RETURNING *
)
 INSERT INTO __test_result(orderid) VALUES ((SELECT orderid FROM upt))

需要更新和记录数据,得到这个错误

ERROR: column "orderid" does not exist Hint: There is a column named 
"orderid" in table "__test_result", but it cannot be referenced from this part of the query.

如何在表格中插入所有“upt”行?它必须看起来

"upt.orderid","jsonb for that orderid"

对于每个订单,jsonb 必须从具有相同 orderid 的“upt”列创建

【问题讨论】:

    标签: sql postgresql sql-insert


    【解决方案1】:

    如果您想使用select 作为插入源(用于多行),请不要使用values 子句,直接使用选择:insert into .. select ...

    所以在你的情况下:

    WITH upt as (
      UPDATE backend."orders" 
         SET "statusId" = 5
      WHERE "userId" IN (177962,88265) 
        and "statusId" IN (0,1,2,3,4) 
      RETURNING *
    )
    INSERT INTO __test_result(orderid) 
    SELECT orderid 
    FROM upt;
    

    【讨论】:

    • 好的,这个呢? INSERT INTO __test_result(orderid,prop) SELECT "orderId",jsonb_build_object('4',4) as prop FROM upt
    • 同样的东西,只有两列
    猜你喜欢
    • 2016-06-19
    • 2014-08-08
    • 2019-11-04
    • 1970-01-01
    • 2015-07-13
    • 2019-06-21
    • 2019-01-14
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多