-3

I couldn't find this anywhere and I am having a hard time to fix this.

Here is my SQL query:

SELECT id, subject, content, description, date, image
FROM articles
WHERE id > $lastid
ORDER BY id ASC
LIMIT 5

This give me output in a following order id 11,12,13,14,15.

Is there a way to switch this output so it would be id 15,14,13,12,11?

2
  • Add another query to use this as a subquery and sort the other way. Commented Feb 12, 2018 at 14:30
  • Use DESC instead of ASC. Commented Feb 12, 2018 at 14:31

1 Answer 1

1
SELECT * FROM (
    SELECT id, subject, content, description, date, image FROM articles WHERE id > $lastid
    ORDER BY id ASC
    LIMIT 5
) t
ORDER BY id DESC

The documentations tells you that there are two ways to sort: ascending and descending. The corresponding sql elements are:

  • ORDER BY [Field] ASC for ascending and
  • ORDER BY [Field] DESC for descending.
Sign up to request clarification or add additional context in comments.

3 Comments

This give me this output id 20-16 as I got newer articles in DB. Basically I ve got query asking for Newer articles so I grab first article id that was used and ask for articles where id > $firstid
You didn't mention that in your question. Edited my answer to reflect that
@VojtěchŠafařík, don't forget to upvote/accept the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.