Forum Moderators: coopster

Message Too Old, No Replies

Order the last 5 records

order the last 5 records

         

danym

11:30 am on Feb 16, 2008 (gmt 0)

10+ Year Member



Hello there,
I am new in webmasterworld, i've been checking it for about a year already.
----------------------------

I am using MySQl database and i would like to know if it's possible to order the last 5 records in a database.

In fact, i m creating a small news CMS and trying to order the last 5 articles by position_id.

select * from articles order by id DESC, position ASC limit 5

is not working. any other way to do that where i can order only the last 5 articles from the articles table.

Thanks in advance.

Dany

omoutop

11:57 am on Feb 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



welcome to webmasterworld

shouldnt this be ORBER BY position_id DESC LIMIT 5?
(instead of order by id DESC, position ASC limit 5)

I dont think you can have two diferent order types (asc, desc) in same query.

In case you definetly want both of them, heres is a solution
- create a query to get last 5 id
- use those 5 ids in a second query with different order clause

example:
from 1st query you will create a string similar to $my_string="5,10,23,67,87";

and in second query: sql=select [whatever] from [table] where id IN ($my_string) ORDER BY [something]

danym

12:37 pm on Feb 16, 2008 (gmt 0)

10+ Year Member



there should be a solution other than using 2 queries.
i just need to selec the last 10 records by id and order them by position.
could be done using a subquery or something?

omoutop

1:39 pm on Feb 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



according to this article [dev.mysql.com], you can sort two colums with different order by (one ASC, one DESC).

But i havent manage to do this in any project of mine.
So, i stick to dual queries.

Sorry i couldnt provide more info.

danym

9:02 am on Feb 17, 2008 (gmt 0)

10+ Year Member



Thanks for your reply, i tried the 2 queries solution and it worked great. :)

phranque

9:25 am on Feb 17, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], danym!