MYSQL tip LAST_INSERT_ID

  • 2020-03-31 19:23:44
  • OfStack

In fact, there is a difference between the two: LAST_INSERT_ID() returns the id of the bigint value. Instead, mysql_insert_id returns an int. If you are
The id of unsigned int, or bigint. So, maybe the return is wrong. Instead, use LAST_INSERT_ID().

And some of my friends, I don't know what's going on, but LAST_INSERT_ID() is going to return the ID of AUTO_INCREMENT.
If AUTO_INCREMENT is not set in the table structure, it cannot be returned.

For others, the return is still 0. Check to see if you used insert delay. In this case, the immediate return id value is not returned.

Many people like to use select Max (id)... To replace this last_insert_id, and in fact, select Max of id is not thread-safe, which is probably,
Other threads insert new data, so you can't find the ID you inserted last time. And last_insert_id is corresponding to a mysql connect, which is
Corresponding to your current thread, it will not be disturbed by other threads. If you have some weird errors in your database, like, you know, you're supposed to be updating information about data A,
As a result, B data is updated, and sometimes it is correct, sometimes it is incorrect, and sometimes it is very incorrect when there are too many people. So let's see if we use select Max of id.

Related articles: