I have the following query:
SELECT id, title, adcontent, adtargetURL, locationpage,
locationarg, locationid, displayorder, visible, schedstart,
schedstop, created, modified, createdby, modifiedby
FROM ads
ORDER BY locationpage, locationarg, locationid, title
I need to order the fields the following way:
- sort by locationpage with any fields with a value of 'all' first, then the rest in ascending order
- then by locationarg with any NULL or empty string values first, then the rest in asc
- then by locationid with any NULL or 0 values first, the rest in asc
- and within those, sort by displayorder of '1' first, then NULL, then '2'
- and lastly, by title in ASC if any of those manage to all be the same
What should my ORDER BY look like to achieve this?
Here's what I have so far: (updated from below)
ORDER BY locationpage='all',
locationpage ASC,
locationarg ASC,
locationid ASC,
displayorder='1',
ISNULL(displayorder),
displayorder='2',
title ASC
...but this isn't working!