Search code examples
mysqlsql-order-by

special ORDER BY in a Mysql request


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 :)


Solution

  • You may use the following logic:

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