LAST_INSERT_ID( ) returning multiple rows of 0?

后端 未结 5 635
独厮守ぢ
独厮守ぢ 2020-12-17 06:17

Working in phpMyAdmin for now:

order table strucure:

OrderID     int(11)  auto_increment
CustomerID  varchar(50)
BillAddr    varchar(200         


        
相关标签:
5条回答
  • 2020-12-17 06:50

    I had the same issue, solution was do the the query at the same time. In other words first query and the second(last_insert_id) query should be executed at the same time. Not as two different execution.

    0 讨论(0)
  • 2020-12-17 06:56

    LAST_INSERT_ID() returns the id of the last inserted row and is not bound to any table. So if you create a new row:

    INSERT INTO table VALUES('a', 'b', 'c');
    

    It will return the last id (whatever value the new primary key has).

    SELECT LAST_INSERT_ID();
    => 123 
    

    For details, please take a look at the manual:

    LAST_INSERT_ID() (with no argument) returns a BIGINT (64-bit) value representing the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this:

    If you just want to get last ID in a table, you can do it like this:

    SELECT id FROM table ORDER BY id DESC LIMIT 1;
    
    0 讨论(0)
  • 2020-12-17 07:00

    As stated in the manual:

    LAST_INSERT_ID() (with no argument) returns a BIGINT (64-bit) value representing the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this:

    mysql> SELECT LAST_INSERT_ID();
            -> 195
    0 讨论(0)
  • 2020-12-17 07:04

    by indicating a table, you repeat the select statement for EVERY row in said table.

    it does not affect the result of your query, other than to repeat the same value.

    if running in phpmyadmin, you might want to check that Persistent Connections is set to TRUE, otherwise you will ALWAYS receive 0 as a result of LAST_INSERT_ID().

    0 讨论(0)
  • 2020-12-17 07:09

    Do it like this:

    INSERT INTO `one`(`id`, `name`) VALUES (NULL,'shivam');
    select last_insert_id();
    

    Perform no other query after insert and they both should execute together

    0 讨论(0)
提交回复
热议问题