1

I want to insert into two tables. I want to insert a name in to 'Names' and then insert a DVD into 'DVDs', using the id of the name inserted into 'Names'.

Names
id | name
1  | john

DVDs
id | title    | user_id
1  | Star Wars| 1

Updating the DVDs table depends on the insert id of the first insert.

Is there a way to do this in one query or would it need separating in to two quires?

panthro
  • 22,779
  • 66
  • 183
  • 324
  • what exactly do you want? update or insert? what is your desired output in the example above? – John Woo May 30 '13 at 14:18
  • See this post that should answer your question [LAST_INSERT_ID() MySQL][1] [1]: http://stackoverflow.com/questions/3837990/last-insert-id-mysql – liebs19 May 30 '13 at 14:19

1 Answers1

0

I hope this is what you mean:

INSERT INTO Names (name) VALUES ('john');
SET @last_id_Names = LAST_INSERT_ID();
INSERT INTO DVDs (title, user_id) VALUES (@last_id_Names, 'Star Wars');
MHarteveld
  • 167
  • 2
  • 8