简体   繁体   中英

Multiple returend values of SQL sub-query

I am using PostgreSQL 9.1, I wrote the following SQL statement:

INSERT INTO "Tracking" VALUES 
((SELECT "studentID" FROM "Student" WHERE "studentClass"='2'),false,4,false);

The issue is that the sub-query :

SELECT "studentID" FROM "Student" WHERE "studentClass"='2'

returned more than one value, and it is supposed to do that(I want to execute the main query per each returned value of sub-query), but by this way the Query will not be executed. Any Idea?

Try this:

INSERT INTO "Tracking" 
SELECT "studentID",false,4,false
FROM "Student" WHERE "studentClass"='2'

Then use INSERT INTO... SELECT statement

INSERT INTO "Tracking" 
SELECT "studentID" , false , 4 , false
FROM   "Student" 
WHERE  "studentClass" = '2'

One thing to make sure about this statement is to make sure that table Tracking contains only 4 column or else you will getting number of columns mismatched with the supplied value. If for instance you have more than 4 columns, define the column name on the INSERT clause on which you want to save those values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM