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