Avatar of peps03
peps03
 asked on

Sorting mysql query by 2 conditions

Hi,

I'm trying to sort a mysql query by 2 conditions: Deadline in future or deadline in past.

The table has a column named 'deadline'. When outputting the result i would like to first show all the deadlines in the future with the nearest first, when all these are outputted, the ones with a deadline in the past should be outputted, with the most recently 'expired' at the top.

Like this, with today being 7 July 2012:
14 July 2012
18 July 2012
05 July 2012
01 July 2012

the results are paginated, so i would like to be able to achieve this with 1 query if that is possible..

Many thanks!
PHPMySQL ServerDatabases

Avatar of undefined
Last Comment
peps03

8/22/2022 - Mon
Neil Russell

Just as an asside, what about the current day? You only mention dealines in the future and in the past. What happens to those who's deadline is Today?
peps03

ASKER
first!
07 July 2012
14 July 2012
18 July 2012
05 July 2012
01 July 2012
Ray Paseur

The order is kind of counterintuitive if you want to achieve this in one query.  My guess is that you could use some kind of temporary table (engine=memory) and make two select queries to load the temporary table with the rows in the correct order.  Then you could make a select from the temporary table to get a single results set for pagination.
Your help has saved me hundreds of hours of internet surfing.
fblack61
peps03

ASKER
How should i do something like that Ray?
peps03

ASKER
could the use of CASE help?
or is it possible to combine 2 SELECT statements in 1 query?
ASKER CERTIFIED SOLUTION
hielo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
peps03

ASKER
Thanks hielo!
That works, but the ordering is now not sorted by deadline.

Adding  this: ORDER BY deadline ASC to the end of the query sorts the entire query, putting the passed deadlines back on top again.

adding ORDER BY deadline ASC to both parts of the query results in an error..
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
peps03

ASKER
This did the trick (below EDIT:):
http://stackoverflow.com/questions/7986455/php-mysql-union-order-by

Thanks for the help! UNION did the trick!