2012-06-19 80 views
6

這是我的SQL查詢:SQL查詢結果安排問題

select name,description 
from wp_widget_custom 
where name in ('hemel-hempstead','maidenhead', 
    'guildford','bromley','east-london','hertfordshire','billericay','surrey') 

我得到的結果是這種形式:

name   description 
hemel-hempstead Loreum ipsim 
east-london  Loreum ipsim 
bromley   Loreum ipsim BROMLEY 
billericay  Loreum ipsim 
maidenhead  Loreum ipsim maidenhead 
hertfordshire Loreum ipsim HERTFORDSHIRE 
guildford  loreum ipsum Guildford 
surrey   loreum ipsum surrey 

我想要的結果來安排它傳遞的方式在查詢中:

hemel-hempstead ,maidenhead',guildford,bromley,east-london...... 

幫助表示讚賞,謝謝。

回答

6

按字段:

select name, description 
from wp_widget_custom 
where name in ('hemel-hempstead','maidenhead','guildford','bromley','east-london','hertfordshire','billericay','surrey') 
order by field(name, 'hemel-hempstead','maidenhead','guildford','bromley','east-london','hertfordshire','billericay','surrey') 
+0

+1更容易產生比我的答案:) – Andomar

+0

巨大的我從來不知道,我們可以安排這樣的感謝 –

+0

尼斯,瞭解到項目新鮮玩意 :) –

1

你可以使用過濾inner join,而不是一個where ... in條款。這允許您指定一個訂單,然後你就可以在order by子句中引用:

select wc.name 
,  wc.description 
from wp_widget_custom wc 
join (
     select 1 as nr, 'hemel-hempstead' as name 
     union all select 2, 'maidenhead' 
     union all select 3, 'guildford' 
     union all select 4, 'bromley' 
     union all select 5, 'east-london' 
     union all select 6, 'hertfordshire' 
     union all select 7, 'billericay' 
     union all select 8, 'surrey' 
     ) as filter 
on  filter.name = wc.name 
order by 
     filter.nr