Jump to content

Ordering by a specific value without using a filesort.


Recommended Posts

This problem has been nagging me for a while now. I have multiple queries that contain this at the end:

 

ORDER BY done = 0 DESC, posted DESC

 

Which makes the results ordered in the form 0, 1, -1 which is fine. Now, because of the = 0, MySQL has to perform a filesort. Is there any way around this?

 

Also, is there any way to order the rows by 'done' = 0 first, but then disregard the 'done' state and just sort by posted?

If you sort by an expression, you're going to get a filesort --- nothing you can do about that at the moment.

 

But yes, if you sort by "ORDER BY IF( done = 0, 1, -1 ) DESC, posted DESC", you'll effectively get what you want.

Oh, ok. That sorts things out (pardon the pun).

 

How about using multiple queries (or joins) instead? If I wanted to get all of the done = 0 first, then all others and have them all sorted by posted DESC, how would I do that?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.