LAST_INSERT_ID( ) returning multiple rows of 0?

后端 未结 5 756
独厮守ぢ
独厮守ぢ 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: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;
    

提交回复
热议问题