ORDER BY “ENUM field” in MYSQL

前端 未结 5 738
忘掉有多难
忘掉有多难 2020-12-04 23:40

There is a field \'noticeBy\' enum(\'email\',\'mobile\',\'all\',\'auto\',\'nothing\') NOT NULL DEFAULT \'auto\'. As it known ordering by ENUM field performs relative to its

5条回答
  •  Happy的楠姐
    2020-12-05 00:18

    You can define your order however you wish:

    ORDER BY CASE noticeBy
               WHEN 'email' THEN 1
               WHEN 'mobile' THEN 2
               WHEN 'all' THEN 3
               WHEN 'auto' THEN 4
               ELSE 5
             END
    

    This will return the rows in the following order: email, mobile, all, auto, nothing.

提交回复
热议问题