4

我正在尝试按ticket_priority_id.

ID 被分类为ASAP, HIGH, MED, LOW

当我按它订购时,ticket_priority_id它总是按字母顺序排列(LOW在前面MED)。

我怎样才能做到这一点,以便我可以按顺序ticket_priority_id但不是按字母顺序而是按重要性排序。我要订单(从上到下)ASAP- HIGH- MED-LOW

4

2 回答 2

8

您可以像这样在您的订单中使用 case 语句

ORDER BY 
   CASE WHEN ticket_priority_id = 'ASAP' THEN 1
        WHEN ticket_priority_id = 'HIGH' THEN 2
        WHEN ticket_priority_id = 'MED' THEN 3
        WHEN ticket_priority_id = 'LOW' THEN 4
        ELSE 5
   END
于 2013-04-04T20:05:50.000 回答
5

您可以使用FIELD功能:

ORDER BY
  FIELD(ticket_priority_id, 'ASAP', 'HIGH', 'MED', 'LOW')

请注意,如果ticket_priority_id以下列表中不存在,它将返回 0,因此您可能还需要添加:

ORDER BY
  ticket_priority_id NOT IN ('ASAP', 'HIGH', 'MED', 'LOW'),
  FIELD(ticket_priority_id, 'ASAP', 'HIGH', 'MED', 'LOW')

将没有优先级的行放在底部而不是顶部。

于 2013-04-04T20:12:11.360 回答