有一个名为...的列,它不能从查询子查询的这一部分引用

时间:2018-06-07 10:32:39

标签: sql postgresql sql-insert

 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必须从" upt"具有相同orderid的列

1 个答案:

答案 0 :(得分:2)

如果要使用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;