Order something and then random?

I have a query, something like this:

..ORDER BY photo.sort_order DESC, profile.description DESC, RAND()

So, this means that first records with photos are displayed, and then those that have descriptions. Inside this, I want the order to be random (therefore, if there are ten entries with photos, they should always be on top, but ordered randomly)

The above does not work, and I know that rand () is not very efficient. What would be another simple solution?

+4
source share
1 answer

You probably want to change your selection to accept desc txt from the equation, because you are only interested in its value.

  select photo.sort_order, profile.description is not null as desc_order, profile.description, rand() as r from photo photo, profile profile order by photo.sort_order desc, desc_order desc, r 
+2
source

All Articles