2012-06-19 13 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