HAMMAD ALI
HAMMAD ALI

Reputation: 175

Subquery in Insert

I am trying to use subquery in insert query but i'm getting error. How can i solve this?

insert into classes_has_students (id,student_id,class_id) 
values (
    (select id from students where first_name = 'Subhan'),
    (select id from classes where class_name = 'FSC')
)

Upvotes: 0

Views: 29

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269763

You have an extra id in the columns list. Presumably, it is assigned automatically so you can leave it out:

insert into classes_has_students (student_id, class_id) 
    values ( (select id from students where first_name = 'Subhan'),
             (select id from classes where class_name = 'FSC')
           )

Otherwise, you need to give it a value:

insert into classes_has_students (id, student_id, class_id) 
    values ( 42,
             (select id from students where first_name = 'Subhan'),
             (select id from classes where class_name = 'FSC')
           )

Upvotes: 1

Related Questions