MySQL ORDER BY [custom SET field value]

后端 未结 5 1103
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 04:34

I hope there\'s a simple solution for this:

I have a table where each row has it\'s own status (SET type field). Statuses can be:

  • offline<
相关标签:
5条回答
  • 2020-12-17 04:41
    SELECT * FROM table ORDER BY FIELD(status,'offline','available','busy','distance')
    

    see Mysql order by specific ID values

    0 讨论(0)
  • 2020-12-17 04:50

    You could also do something like this, if reordering the SET values in impractical:

    ... ORDER BY CASE `status` 
                    WHEN 'available' THEN 1
                    WHEN 'busy' THEN 2
                    WHEN 'distance' THEN 3
                    WHEN 'offline' THEN 4
                 END
    
    0 讨论(0)
  • 2020-12-17 04:53

    Thought I'd add another way to order by custom field values,

    ORDER BY FIND_IN_SET(status, 'available,busy,distance,offline')
    

    (If the given strings contain a quote simply escape it)

    0 讨论(0)
  • 2020-12-17 05:01

    Nevermind.

    The trick is saving SET values at the right order.

    0 讨论(0)
  • 2020-12-17 05:03

    Simpler than the solutions above:

    ORDER BY CONCAT(status)
    
    0 讨论(0)
提交回复
热议问题