0

How do I preserve the last_insert_id() into a specific record?
Here are my queries:

INSERT INTO student(student_name) VALUES ('ramgopal')`;   
SELECT `last_insert_id()`;   
INSERT INTO grades (student_id,grade) VALUES (last_insert_id(),'A');   
INSERT INTO class (semester,day,time) VALUES ('spring 2015','tuesday','12');   
SELECT last_insert_id();  
insert INTO grades(class_id) VALUES (last_insert_id());   

In grade table I got grade_id, class_id, student_id and grade records.

When I use the last_insert_id() again it is just inserting the latest id and the previous id is not inserting. So I would like to preserve the initial last_insert_id for further use.

John Keyes
  • 5,479
  • 1
  • 29
  • 48
sairam
  • 11
  • 5
  • Check out this; I think it answers your question: http://stackoverflow.com/questions/3837990/last-insert-id-mysql – nomistic Apr 20 '15 at 22:12
  • But this is inserting values one after the other not simultaneously. – sairam Apr 20 '15 at 22:26
  • it might be easier to do with your application layer; you could easily assign the value to a variable, and then just call that variable when you need it, and reset with new rows – nomistic Apr 20 '15 at 22:39
  • 1
    `set @student_id = last_insert_id();` `insert into grades (student_id, grade) values (@student_id, 'A');` – pala_ Apr 20 '15 at 22:56

0 Answers0