From: Michael Dykman Date: December 27 2009 5:00pm Subject: Re: last_insert_id List-Archive: http://lists.mysql.com/mysql/219929 Message-Id: <814b9a820912270900o77bea6d0j5e21be427c535f10@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable last_insert_id() returns the last id auto-incremented in *the current session*. If you disconnect and reconnect, it can not be retrieved. - michael dykman On Sun, Dec 27, 2009 at 11:42 AM, Victor Subervi wrote: > On Sun, Dec 27, 2009 at 11:27 AM, Mattia Merzi wr= ote: > >> 2009/12/27 Victor Subervi : >> > mysql> select * from products; >> [...] >> > mysql> select last_insert_id() from products; >> [...] >> > Now, I was expecting 1, not 0! What up? >> >> [...] LAST_INSERT_ID() (no arguments) returns the first >> automatically generated value successfully inserted for >> an AUTO_INCREMENT column as a result of the most >> recently executed INSERT statement. [...] If no rows >> were (successfully) inserted, LAST_INSERT_ID() returns 0. >> > > But it *is* auto incremented! > > mysql> describe products; > +--------------------------+---------------------------------------------= ---------------------------------------------------------------------------= ---------------------------+------+-----+------------+----------------+ > | Field =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > Type > | Null | Key | Default =A0 =A0| Extra =A0 =A0 =A0 =A0 =A0| > +--------------------------+---------------------------------------------= ---------------------------------------------------------------------------= ---------------------------+------+-----+------------+----------------+ > | ID =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | tinyint(5) > unsigned > | NO =A0 | PRI | NULL =A0 =A0 =A0 | auto_increment | > | SKU =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > varchar(40) > | NO =A0 | UNI | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | Category =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | > varchar(40) > | YES =A0| =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | Name =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | > varchar(50) > | NO =A0 | =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | Title =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > varchar(100) > | NO =A0 | =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | Description =A0 =A0 =A0 =A0 =A0 =A0 =A0| > mediumtext > | NO =A0 | =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | Price =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > float(8,2) > | YES =A0| =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | SortFactor =A0 =A0 =A0 =A0 =A0 =A0 =A0 | > int(4) > | YES =A0| =A0 =A0 | 500 =A0 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= | > | Availability =A0 =A0 =A0 =A0 =A0 =A0 | > tinyint(1) > | NO =A0 | =A0 =A0 | 1 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0| > | OutOfStock =A0 =A0 =A0 =A0 =A0 =A0 =A0 | > tinyint(1) > | NO =A0 | =A0 =A0 | 0 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0| > | ShipFlatFee =A0 =A0 =A0 =A0 =A0 =A0 =A0| > float(5,2) > | NO =A0 | =A0 =A0 | 10.00 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | ShipPercentPrice =A0 =A0 =A0 =A0 | tinyint(2) > unsigned > | NO =A0 | =A0 =A0 | 5 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0| > | ShipPercentWeight =A0 =A0 =A0 =A0| tinyint(2) > unsigned > | NO =A0 | =A0 =A0 | 2 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0| > | Associations =A0 =A0 =A0 =A0 =A0 =A0 | > varchar(40) > | NO =A0 | =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | TempPrice =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > tinyint(1) > | NO =A0 | =A0 =A0 | 1 =A0 =A0 =A0 =A0 =A0| =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0| > | LastDatePrice =A0 =A0 =A0 =A0 =A0 =A0| > date > | NO =A0 | =A0 =A0 | 2000-01-01 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | Weight =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | > float(7,2) > | NO =A0 | =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | Metal =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| enum('14k gold','18k gol= d','white > gold','silver','tungsten','titanium') > | NO =A0 | =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | PercentMetal =A0 =A0 =A0 =A0 =A0 =A0 | tinyint(2) > unsigned > | NO =A0 | =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | pic0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | > mediumblob > | YES =A0| =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | pic1 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 | > mediumblob > | YES =A0| =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | sizes =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > set('Extra-small','Small','Medium','Large','XLarge','XXLarge','XXXLarge') > | YES =A0| =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > | colorsShadesNumbersShort | > set('blue:333399','gray:465945','purple:50404D','navy-blue:CC7722','fuchs= ia:FF77FF','aqua:7FFFD4','maroon:B03060','black:0000FF','yellow:9ACD32') > | YES =A0| =A0 =A0 | NULL =A0 =A0 =A0 | =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0| > +--------------------------+---------------------------------------------= ---------------------------------------------------------------------------= ---------------------------+------+-----+------------+----------------+ > 23 rows in set (0.00 sec) > > mysql> > --=20 - michael dykman - mdykman@stripped "May you live every day of your life." Jonathan Swift Larry's First Law of Language Redesign: Everyone wants the colon.