0

I want to insert multiple rows in one query like this:

insert into EducationInfo (student_id, school_id)
values (
    (select id
     from STUDENT
     where iin in (select distinct iinplt
                   from TEST_STUDENT
                   where id_university = 9)), 23421
       )

Of course it is impossible. I know that I can do it like this:

 values(1,23421),
 values(2,23421)...

but as you can see I don't know ids from student_id column and there are too many ids. Is there any possible way to do it?

2 Answers 2

5

Use insert . . . select:

insert into nedb.EducationInfo (student_id, school_id)
     select id, 23421
     from nedb.student
     where iin in (select iinplt
                   from IMPORT_DATA.esuvo_students
                   where id_university = 9
                  );

Note that select distinct is unnecessary when using in.

Sign up to request clarification or add additional context in comments.

Comments

1

Use insert ... select ... join, that way you can manipulate your join to be something far more elaborate that just "in"

insert into nedb.EducationInfo (student_id, school_id)
 select distinct s.id, 23421
 from nedb.student s
 join (SELECT distinct iinplt FROM IMPORT_DATA.esuvo_students 
       WHERE id_university = 9 ) es ON s.iin = es.iinplt

make sure you don't have any many to many JOIN matches on your ON statement, otherwise it might slow the query down

1 Comment

Very nice solution! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.