Use the new value to retrieve the entire record, without even knowing what the id is : LAST_INSERT_ID « Function « SQL / MySQL






Use the new value to retrieve the entire record, without even knowing what the id is

       
mysql>
mysql> CREATE TABLE insect
    -> (
    ->     id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    ->     PRIMARY KEY (id),
    ->     name VARCHAR(30) NOT NULL, # type of insect
    ->     date DATE NOT NULL, # date collected
    ->     origin VARCHAR(30) NOT NULL # where collected
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO insect (name,date,origin) VALUES('moth','2010-09-14','windowsill');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT * FROM insect WHERE id = LAST_INSERT_ID( );
+----+------+------------+------------+
| id | name | date       | origin     |
+----+------+------------+------------+
|  1 | moth | 2010-09-14 | windowsill |
+----+------+------------+------------+
1 row in set (0.00 sec)

mysql>
mysql> drop table insect;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Save the result from LAST_INSERT_ID( ) after creating a new record in a table that has an AUTO_INCREMENT column to a variable
2.Each time a conference registration form is received, enter the attendee information into the attendee table.
3.Update with LAST_INSERT_ID
4.Using LAST_INSERT_ID() in insert statement
5.To see what the new seminar records look like, use the LAST_INSERT_ID() value to retrieve them: