15

I have to get last insert id from a specific inserted table?. Lets say i have this code:

INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2');
INSERT INTO blahblah2 (test1, test 2) VALUES ('test1', 'test2');
INSERT INTO blahblah3 (test1, test 2, lastid) VALUES ('test1', 'test2', last id of blahblah);

How do i get the insert id of table blahblah in table blahblah3? LAST_INSERT_ID() only gives you the last insert id

Regards, Simon :)

2
  • In your example simply switching the INSERT INTO blahblah2 and the INSERT INTO blahblah3 line would do the trick.
    – VolkerK
    Commented Jan 10, 2013 at 10:16
  • I can't do that in my coding, sry :/
    – simon
    Commented Jan 10, 2013 at 10:32

6 Answers 6

15

You can use LAST_INSERT_ID() function. Try this:

INSERT INTO blahblah (test1, test2) VALUES ('test1', 'test2');

SELECT LAST_INSERT_ID() INTO @blahblah;

INSERT INTO blahblah2 (test1, test2) VALUES ('test1', 'test2');

INSERT INTO blahblah3 (test1, test2, lastid) VALUES ('test1', 'test2', @blahblah);
0
14

Is this what you are looking for?

SELECT id FROM blahblah ORDER BY id DESC LIMIT 1
0
9

If you want to do it in a single statement use:

INSERT INTO blahblah3 (test1, test2, lastid)
VALUES ('test1', 'test2', (select MAX(id) FROM blahblah));

This way you don't need to save any variables beforehand which assures you'll get the latest ID at that exact moment.

9
  • I tried your example in this way, but it does not work: $mysqli->query("INSERT INTO products SET product_title='test'"); $mysqli->query("INSERT INTO tags SET tag_values='test'"); $mysqli->query("INSERT INTO iamges (images_path, product_id) VALUES ('blah', SELECT MAX(product_id) FROM products)'");
    – simon
    Commented Jan 10, 2013 at 10:37
  • You forgot the brackets around the select, that's important. Use $mysqli->query("INSERT INTO iamges (images_path, product_id) VALUES ('blah', (SELECT MAX(product_id) FROM products))");. Also at the end you wrote [...] products)'");, I think it should be [...] products)"); (remove the ').
    – flu
    Commented Jan 10, 2013 at 10:44
  • 1
    Sorry to bring this back up, but can you tell me what should be outputted? I've got MAX(ID) and in PHPMyAdmin it brings up the required result (2) but I cannot get this in a script. Can you give some code to show how I could print the result (2)? Thanks alot :)
    – Mattios550
    Commented Aug 15, 2013 at 14:00
  • @Mattios550 Could you please be more specific? Why do you want to print the result? This code is for insertion :) If it's a matter of the scripting language you should think about asking it in the sections for that particular language.
    – flu
    Commented Aug 19, 2013 at 11:50
  • @flu my aim is to simply get the largest entry in a column - in this case, 'id' - obviously, these are sequential. I know there are other ways, but I'd like to use MAX(ID) to get the highest ID then use SELECT... WHERE id='$id' when $id is MAX(ID) to get the latest entry in a table. Sorry if this is confusing & thanks for your reply :)
    – Mattios550
    Commented Aug 19, 2013 at 16:20
2

You can use mysql_insert_id(); function to get a quick answer.

But if you are using a heavy traffic site, chances of in accurate results exist.

1

You can use LAST_INSERT_ID() function.

INSERT INTO blahblah (test1, test 2) VALUES ('test1', 'test2');
    //this query will return id. Save it in one variable

 select LAST_INSERT_ID()

In short, save the last insert id in one variable and then use it in blahblah3

2
  • How do i do that? I doubt it :/
    – simon
    Commented Jan 10, 2013 at 10:19
  • i was trying to tell you exactly what Saharsh Shah has mentioned in his answer. The only difference is that he has presented his answer in a better way. Commented Jan 10, 2013 at 10:39
0

you can also find last id by query in codeigniter As

$this->db->order_by('column name',"desc");
   $this->db->limit(1);
 $id=  $this->db->get('table name');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.