Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is a column named ... it cannot be referenced from this part of the query sub query

 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))

Need to update and log data,getting this error

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.

How can I insert in table for all "upt" rows?It must look

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

for every order jsonb must be created from "upt" column with same orderid

like image 401
Grigor Avatar asked Jun 07 '18 10:06

Grigor


People also ask

What is excluded in PostgreSQL?

What Is EXCLUDED in PostgreSQL. EXCLUDED is the name the DBMS gives to a special table where we have all the rows proposed for INSERTION present. These rows may be inserted to this table as soon as the INSERT operation runs. This is mostly preceded by the ON CONFLICT DO UPDATE clause, specifically targeting this table.

Which SQL keyword is used to specify the tables that contains the data to be retrieved select table from show?

SELECT statements An SQL SELECT statement retrieves records from a database table according to clauses (for example, FROM and WHERE ) that specify criteria. The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value';

What is used to separate the column names and data types when creating a table using a SQL statement?

In the CREATE TABLE statement, you specify a comma-separated list of column definitions. Each column definition is composed of a column name, column's data type, a default value, and one or more column constraints.

Which of the following commands list columns of a table in MS SQL?

In SQL Server, you can execute the sp_columns command to list all the columns in a table.


1 Answers

If you want to use a select as the source for an insert (for multiple rows) do not use the values clause, use the select directly: insert into .. select ....

So in your case:

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;
like image 127
a_horse_with_no_name Avatar answered Oct 09 '22 19:10

a_horse_with_no_name