Reputation: 570
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:
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!
Upvotes: 4
Views: 772
Reputation: 10712
Your best choice would be to use a calculated field that will generate order id according to your rules.. I'll add an example in a moment..
select
case
when locationpage = "all" then 10
when ISNULL(locationarg) then 20
....
else 50
end as OrderID,
id,
title,
....
FROM ads
ORDER BY OrderID DSEC
Upvotes: 1
Reputation: 9853
Give this a bash:
select id, title, adcontent, adtargetURL, locationpage, locationarg, locationid, displayorder, visible, schedstart, schedstop, created, modified, createdby, modifiedby
from ads
order by case when locationpage='all' then 0 else 1 end,
locationpage,
case when locationarg is null or locationarg='' then 0 else 1 end,
locationarg ASC,
case when locationid is null OR locationid='0' then 0 else 1 end,
locationid,
case when displayorder =1 then 0 when displayorder is null then 1 when displayorder = 2 then 2 else 3 end,
title;
Upvotes: 0