0

I have a table which has id, name ,surname columns. When I add a new line to table, id increases by 1 since its AI and PK. Now how I get back latest id variable with OUTPUT command?

"INSERT INTO table (name, surname) VALUES ('mike', 'hensen') OUTPUT ?????? how to continue ????"

edit LAST_INSERT_ID() is not a very good method since in a big webpage there could be a lot adding per second.

4
  • 1
    possible duplicate of MySQL INSERT with OUTPUT like MSSQL Commented Jul 18, 2015 at 16:12
  • okay but how i am gonna echo out this value? Commented Jul 18, 2015 at 16:20
  • SELECT @yournamedvariable for example. Commented Jul 18, 2015 at 16:20
  • can you read 4th comment on your question and answer cause this is a big issue. Commented Jul 18, 2015 at 16:22

1 Answer 1

3

I'm pretty sure that LAST_INSERT_ID() is exactly what you want. It returns the last id inserted on a per connection basis, not the last one inserted (documented here). Presumably, different web users would have different connections, so using the function does what you want.

If you want the last id that was inserted over all connections, but not necessarily from your most recent insert, then you can look at the auto_increment value in the metadata.

Sign up to request clarification or add additional context in comments.

2 Comments

(SET @web_id = LAST_INSERT_ID()); how i am gonna echo out the web_id value in php?
@user3301042 . . . You do it with a query following the insert: select last_insert_id().

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.