Forums
This topic is locked
SELECT Statement Ordering
Posted 14 Aug 2006 20:03:08
1
has voted
14 Aug 2006 20:03:08 Digital Design posted:
<font face='Tahoma'>Hello Forum!
I'm relatively new to SQL Queries and have a specific formatting that I would like to apply to a recordset result and am not quite sure how to accomplish it. Any help would be greatly appreciated! Here's my challenge:
I would like to get a result of 5-7 records (varies on page).
In the table I have a Year column and an ID column.
Am using a repeating region to loop through the recordset and display the data.
Would like to sort by ID / DESC, because it is an autonumber field and would take the latest 5-7 records in the database.
Instead of having the records show up as Year 10, Year 9, Year 8, Year 7, Year 6, I would like it to return the oldest of the five to the newest as in: Year 6, Year 7, Year 8, Year 9, Year 10. The oldest year will fall off as they add years annually (FIFO).
Using Dreamweaver 8, ASP, MS Access DB to develop on and migrating to something more robust once the code is fleshed out.
Any thoughts, inspirations and advice? Is there something simple that I'm overlooking?
Thanks much,
Monica
</font id='Tahoma'>
Edited by - TwisterMK on 22 Sep 2006 06:04:57
Replies
Replied 15 Aug 2006 12:53:38
15 Aug 2006 12:53:38 Roddy Dairion replied:
Well i don't know if this can be done, in mysql but you can play around in the loop function by using arrays. For example at the end of your mysql query you put
<pre id=code><font face=courier size=2 id=code> order fieldname DESC limit 7 </font id=code></pre id=code>
this will show the last 7 record starting with the newest of those 7. But then in your code you store all the result in an array. I don't know how to work with asp but in php i would basically go for something like that
<pre id=code><font face=courier size=2 id=code>
$extract = array($row['fieldname']);
for ($i = count($extract); $i > 0 ; $i--)
{
echo $extract[$i];
}
</font id=code></pre id=code>
This will basically start with the highest record and set it to display first and the rest will follow automatically.
Of course this is a simple version there might be more to add to it but like i said am not really into asp.
<pre id=code><font face=courier size=2 id=code> order fieldname DESC limit 7 </font id=code></pre id=code>
this will show the last 7 record starting with the newest of those 7. But then in your code you store all the result in an array. I don't know how to work with asp but in php i would basically go for something like that
<pre id=code><font face=courier size=2 id=code>
$extract = array($row['fieldname']);
for ($i = count($extract); $i > 0 ; $i--)
{
echo $extract[$i];
}
</font id=code></pre id=code>
This will basically start with the highest record and set it to display first and the rest will follow automatically.
Of course this is a simple version there might be more to add to it but like i said am not really into asp.