Conditional sorting in MySQL?

Posted by serg555 on Stack Overflow See other posts from Stack Overflow or by serg555
Published on 2009-07-05T16:26:42Z Indexed on 2010/03/14 21:25 UTC
Read the original article Hit count: 166

Filed under:
|
|

I have "tasks" table with 3 fields:

  1. date
  2. priority (0,1,2)
  3. done (0,1)

What I am trying to achieve is with the whole table sorted by done flag, tasks that are not done should be sorted by priority, while tasks that are done should be sorted by date:

  1. Select * from tasks order by done asc
  2. If done=0 additionally order by priority desc
  3. If done=1 additionally order by date desc

Is it possible to do this in MySQL without unions?

Thanks.

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql