mysql - Adding primary key as a part of INSERT INTO table VALUES query -


just demonstration purposes. have 2 tables "quiz" , "question".

the quiz table contains following columns

  • quiz_id (pk)

  • quiz_title

the question table contains following columns

  • question_id (pk)
  • quiz_id (fk)
  • question_text

i trying insert question text "question_text" in question table.

i can retrieve latest quiz_id , insert question table.

insert question (quiz_id) select max(quiz_id) quiz

but need add question text. tried this:

insert question (question_text) values ("question_text_sample")

but getting error because fk info (quiz_id) not provided in query.

i combine queries above can't figure out working solution.

i tried following:

insert quiz (quiz_title) values ("quiz_title_sample") insert question (quiz_id, question_text) values (last_insert_id(), "question_text")

another try:

insert question (quiz_id, question_text) values (select max(quiz_id) quiz , "question_text_sample")

could please give me directions?

thanks in advance!

you close, should it

insert question (quiz_id, question_text) select max(quiz_id), "question_text_sample" quiz  

more on insert syntax: http://dev.mysql.com/doc/refman/5.5/en/insert.html


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -