0

I am new to my sql and got stuck in a query. The query I am using is as follows;

insert into tableA(fname, lname) values('hello','world'); @id=SELECT LAST_INSERT_ID(); insert into tableB(x,y) values('good', @id); 

but i am getting an error below;

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@id=SELECT LAST_INSERT_ID()' at line 2 

please somebody suggest what is wrong I am doing here, thanks in advance.

2 Answers 2

2

Close.

insert into tableA(fname, lname) values('hello','world'); SET @id = LAST_INSERT_ID(); insert into tableB(x,y) values('good', @id); 

Result:

mysql> insert into tableA(fname, lname) values('hello','world'); Query OK, 1 row affected (0.07 sec) mysql> SET @id = LAST_INSERT_ID(); Query OK, 0 rows affected (0.00 sec) 
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry. That's what I get for copy and pasting your code without a coffee first. I've edited my answer, remove the 'SELECT' from the SET statement!!
What should "Close" mean? Please add some more relevant information to your answer, like a link to the documentation.
Not close as in 'make not open', but close as in, you're very nearly right :)
2

You're missing set

insert into tableA(fname, lname) values('hello','world'); set @id=SELECT LAST_INSERT_ID(); insert into tableB(x,y) values('good', @id); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.