 |
 |
precise SELECT query
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2002
Location: Ft Lauderdale
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
Ah, precision  Thanks a lot.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jul 2002
Location: Hang Loose, Hawaii
Status:
Offline
|
|
</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?
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|