SQL QUERY Result arrangement issue

后端 未结 2 1131
半阙折子戏
半阙折子戏 2021-01-18 12:15

This is my SQL query:

select name,description 
from wp_widget_custom 
where name in (\'hemel-hempstead\',\'maidenhead\',
  \'guildford\',\'bromley\',\'east-l         


        
相关标签:
2条回答
  • 2021-01-18 12:31

    You could use a filtering inner join instead of an where ... in clause. That allows you to specify an order, which you can then reference in the order by clause:

    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
    
    0 讨论(0)
  • 2021-01-18 12:34

    order by field:

    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 讨论(0)
提交回复
热议问题