Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:56:14+00:00 2026-06-03T23:56:14+00:00

I need a MySQL conditional ORDER BY statement for a datetime field. I have

  • 0

I need a MySQL conditional ORDER BY statement for a datetime field. I have a table with posts which I would like to order in the following way: all future posts should be ordered ASC and all historical posts ordered DESC. Eg.:

post_status     post_date     post_title
===========     =========     ==========
future          2012-10-01    Title 1
future          2012-12-01    Title 2
publish         2012-05-01    Title 3
publish         2012-01-01    Title 4

I need something similar to the following SQL…

SELECT post_status, post_date, post_title FROM wp_posts
WHERE post_status IN ('future', 'publish')
ORDER BY post_status ASC,
 CASE post_status 
  WHEN 'future' THEN 'post_date ASC'
  ELSE 'post_date DESC'
 END;

Any hints on how to do this?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:56:16+00:00Added an answer on June 3, 2026 at 11:56 pm

    Try this:

    ORDER BY post_status ASC,
    CASE post_status WHEN 'future' THEN POST_DATE END ASC,
    CASE WHEN post_status <> 'future' THEN post_date END DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a field date_purchased and date_ordered which are datetime fields. Now I need
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a VARCHAR field in mysql with dates separated by commas. Like this:
I have MySQL query which I think needs a subquery. I'd like to count
hi i have players table in mysql . i need to select inactive players
I need a MySql statement that will select all the rows, as well as
I am newbee with PHP and MySQL and need help... I have two tables
Why you may ask? Because i have built the app on mysql and need
I have a mysql table with 3 fields: id, client, group and some data.
I have created a the following strings: set l_project_name = concat(UPPER(a.project_name) like UPPER ('%,projectName,%'));

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.