Home > Software engineering >  special ORDER BY in a Mysql request
special ORDER BY in a Mysql request

Time:06-21

I have a mysql table defined like this: posts(id_post, title, post_date)

post_date is null if the post is not published and is equal to the publication date if the post is published

I would like to be able to list the posts in the following way: first display the posts with a post_date=null, then the posts classified by descending publication date.

How to do ?

Thanks :)

CodePudding user response:

You may use the following logic:

ORDER BY
    post_date IS NULL DESC,  -- nulls first
    post_date DESC;          -- post date descending

CodePudding user response:

For MySql

SELECT *
FROM posts
ORDER BY post_date IS NULL DESC, post_date DESC
  • Related