don
don

Reputation: 4522

Mysql limit and order by

I would like to show the last modified elements in a table, but limit the results to 5, so I did:

SELECT
  Id as Id,Title,LastModified
From
  articles
WHERE
  (Author=70 OR Editor=32 OR Publisher=33) && Disab ="0"

Order By   LastModified

LIMIT 0, 5

The problem with this query is that it is returning the first 5 rows of the table, not the last 5 edited rows...

What am I missing?!

Upvotes: 1

Views: 135

Answers (3)

John Woo
John Woo

Reputation: 263693

You just need to add DESC in the ORDER BY clause to sort the record in descending order.

ORDER BY LastModified DESC

by default, the ORDER BY clause is sorted by ASCENDING order.

Upvotes: 0

Naveen D Almeida
Naveen D Almeida

Reputation: 877

Try this

SELECT
  Id as Id,Title,LastModified
From
  articles
WHERE
  (Author=70 OR Editor=32 OR Publisher=33) && Disab ="0"

Order By   LastModified DESC

LIMIT 0, 5

Upvotes: 0

Matt Busche
Matt Busche

Reputation: 14333

Default ORDER BY is Ascending order. You want descending

SELECT
  Id as Id,Title,LastModified
From
  articles
WHERE
  (Author=70 OR Editor=32 OR Publisher=33) && Disab ="0"

Order By   LastModified DESC

LIMIT 0, 5

Upvotes: 1

Related Questions