Current location: Hot Scripts Forums » Other Discussions » Database » how to select 9 most recent MYSQL entries


how to select 9 most recent MYSQL entries

Reply
  #1 (permalink)  
Old 03-13-07, 09:09 AM
hinch hinch is offline
Wannabe Coder
 
Join Date: Oct 2004
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
how to select 9 most recent MYSQL entries

i can't find anything on assembling a line like this and i can't get it to work that's for sure. I'm looking to display the 9 most recent items in my database, so i call the max ID and i set the limit to nine...but, what? this is the sort of thing i figure:

PHP Code:



function MaxID$table )
{
    
$q mysql_query"SELECT MAX(id) as maxID FROM {$table});
    
$r mysql_fetch_assoc$q );
    return 
$r['maxID'];
}

$maxID MaxID'*table*' );

$query mysql_query"SELECT title FROM table *starting at* id = '{$maxID}' LIMIT 9" ); 
as you can see, i haven't got too far...any help?
Reply With Quote
  #2 (permalink)  
Old 03-13-07, 09:14 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Topic moved to database.

And try this:
sql Code:
  1. SELECT title FROM yourtable ORDER BY id DESC LIMIT 9
Reply With Quote
  #3 (permalink)  
Old 03-14-07, 11:50 AM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
While ordering by the primary key will usually give you the correct order of last inserted rows, the rows of table aren't necessarily stored in order. So using the primary key to do this usually isn't the best idea - it makes most sense to include a "last_updated_datetime" or "created_datetime" column to use for these exact situations when you need to sort by a date value. You are not sorting by the ID - that is just an arbitrary number assigned to the row - you want to sort by some sort of time-sensitive data.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing 2 dates on MySQL entries m_abdelfattah Database 2 02-05-07 04:50 PM
Please help 2 create more efficiently coded dynamic MySQL SELECT pages from PHP/HTML? pilch PHP 2 05-17-06 05:30 AM
mySQL Newbie with nightmare select query (MAX ISSUE) lalapo PHP 4 05-05-06 11:46 AM
SELECT all Entries beginnign with a number from mysql Klesti PHP 3 10-10-04 09:46 AM
MySQL: Can it handle 1 million + entries? archangel PHP 3 08-16-04 03:52 AM


All times are GMT -5. The time now is 04:57 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.