Jump to content

Ordering by a specific value without using a filesort.


mattal999

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.