Re: most recent 5 rows

From: Date: Wed, 08 Aug 2001 01:18:09 +0000
Subject: Re: most recent 5 rows
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-61941@lists.php.net to get a copy of this message
> Or, you could do two steps > > 1) retrieve the last inserted ID, with mysql_last_inserted_ID (or > something like that). > > 2) then, all records with id(s) greater then the_last_id_number minus > five. > > something like > > $most_recent = select mysql_last_id(); > > select * from TABLE where id > '$most_recent - 5'; > > ...or something to that effect. There's probably a more efficient way to > do it, but this guarantees the most recent five. A) This would only work immediately after an INSERT, which may or may not be useful B) Technically, this might not be the most recent 5 if *OTHER* inserts have happened in between your call to mysql_insert_id() and the query for the last 5 in a non-transaction situation. If you have transactions going on, this can get a bit confusing, depending on what kind of transactions etc... A final note: While I can't imaginge how/why/when it would change, I'm not at all certain that an auto_increment ID field is really the best way to get the most recent inserts from a dba point of view. I generally use a time-stamp for that purpose, to be more explicit that I'm getting records created most recently, rather than the coincidence of having the highest IDs. Actually, I guess if the project is small enough or if you're really anticipating millions of records and don't need creation date for other purposes, I'd go with the ID field after all. -- WARNING richard@zend.com address is an endangered species -- Use ceo@l-i-e.com Wanna help me out? Like Music? Buy a CD: http://l-i-e.com/artists.htm Volunteer a little time: http://chatmusic.com/volunteer.htm

« previous php.general (#61941) next »