[英]MySQL query with complex ORDER BY
我有以下查詢:
SELECT id, title, adcontent, adtargetURL, locationpage,
locationarg, locationid, displayorder, visible, schedstart,
schedstop, created, modified, createdby, modifiedby
FROM ads
ORDER BY locationpage, locationarg, locationid, title
我需要按以下方式訂購字段:
我的ORDER BY應該如何實現這一目標?
這是我到目前為止:(從下面更新)
ORDER BY locationpage='all',
locationpage ASC,
locationarg ASC,
locationid ASC,
displayorder='1',
ISNULL(displayorder),
displayorder='2',
title ASC
......但這不起作用!
您最好的選擇是使用一個計算字段,根據您的規則生成訂單ID ..我稍后會添加一個示例..
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
給這個打擊:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.