Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > precise SELECT query

precise SELECT query
Thread Tools
Fresh-Faced Recruit
Join Date: May 2002
Location: Ft Lauderdale
Status: Offline
Reply With Quote
Jul 9, 2002, 11:10 PM
 
I have a SQL query:
select field1, field2 from table order by field3 limit 5;

field3 is a timestamp of when the row was added. The problem is I need the reverse of what ordering by date gives me. It gives me the first five entries, I want the last five. Does anyone know a way to reverse order by? Thanks.
     
Junior Member
Join Date: Jul 2002
Location: Hang Loose, Hawaii
Status: Offline
Reply With Quote
Jul 10, 2002, 03:01 AM
 
here's the only way i can think of doing this right now with PHP:

//find total # of rows in table
$result = mysql_query("select count(*) from table");

$rows = mysql_fetch_row($result);

$rowCount = $rows[0]; //this is the total number of rows

$offset = $rowCount-5;
//since u only want the last five items on the table, offset will be number of rows minus 5.

//final query
mysql_query("select field1, field2 from table order by field3 limit $offset,5");
---

in this example, if u had a table with 67 items, $rowCount would equal 67 and offset'd equal 62.

i only started learning PHP/MySQL ten days ago, so someone else will probably come up w/ a more efficient way of doing it and i'll learn it, too.
Can I have that cookie?
     
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Jul 10, 2002, 04:59 AM
 
select field1, field2 from table order by field3 desc limit 5;

Use 'desc' for descending and 'asc' for ascending.

Make SQL do all the work, not PHP <img border="0" title="" alt="[Wink]" src="wink.gif" />
Computer thez nohhh...
     
Fresh-Faced Recruit
Join Date: May 2002
Location: Ft Lauderdale
Status: Offline
Reply With Quote
Jul 10, 2002, 01:23 PM
 
Ah, precision Thanks a lot.
     
Junior Member
Join Date: Jul 2002
Location: Hang Loose, Hawaii
Status: Offline
Reply With Quote
Jul 10, 2002, 07:31 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by Simon Mundy:
<strong>select field1, field2 from table order by field3 desc limit 5;

Use 'desc' for descending and 'asc' for ascending.

Make SQL do all the work, not PHP <img border="0" title="" alt="[Wink]" src="wink.gif" /> </strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Thanks
Can I have that cookie?
     
   
Thread Tools
Forum Links
Forum 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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:35 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2