Alex Rivera | Logout

MYSQL Select from table, get newest/last 10 rows in table

Asked 2012-02-24T01:58:53.900
13

What's the best, and easiest way to do this? My query currently is:

  SELECT * 
    FROM chat 
   WHERE (userID = $session AND toID = $friendID) 
      OR (userID = $friendID AND toID = $session) 
ORDER BY id 
   LIMIT 10

This shows the first 10 rows though, not the last 10.

EDIT: I Want the last 10 rows (Which yes, DESC does this) However I want them to be returned in ASCENDING order.

Edit
Report

1 Answer

29

to reverse the order (therefore get last 10 instead of first 10), use DESC instead of ASC

EDIT

Based on your comment:

SELECT * FROM (
  SELECT * 
  FROM chat 
  WHERE (userID = $session AND toID = $friendID) 
    OR (userID = $friendID AND toID = $session)  
  ORDER BY id DESC
  LIMIT 10
) AS `table` ORDER by id ASC
answered 2012-02-24T02:01:00.567

Your Answer