Home mysql MySQL Reverse Sort

MySQL Reverse Sort

Author

Date

Category

There is a request of this kind

SELECT a.id, a.author, a.txt, a.timestamp, b.log, b.em
FROM txt_mesg a
LEFT JOIN users b
ON b.id = a.author
WHERE a.tid = '". $ Id."'
ORDER BY a.timestamp DESC
LIMIT 12 "

Messages are displayed in descending order of time, but it is necessary, in ascending order

How can you do this sorting?


Answer 1, authority 100%

Remove DESC or replace it with ASC (but not required)

SELECT a.id, a.author, a.txt, a.timestamp, b.log, b.em FROM txt_mesg a LEFT JOIN users b ON b.id = a.author WHERE a.tid = '". $ Id."' ORDER BY a.timestamp LIMIT 12 "

Answer 2, authority 50%

Apparently, you should also select the last 12 messages, but at the same time change their order. solved by another select level with a different sort:

SELECT *
 FROM (
  SELECT a.id, a.author, a.txt, a.timestamp, b.log, b.em
   FROM txt_mesg a
   LEFT JOIN users b ON b.id = a.author
  WHERE a.tid = '". $ Id."'
  ORDER BY a.timestamp DESC
  LIMIT 12
 ) X
 ORDER BY timestamp

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions