Priority order not in alphabetical order

I am trying to prioritize my request using ticket_priority_id .

Identifiers are classified as ASAP , HIGH , MED , LOW .

When I order ticket_priority_id , it always places it in alphabetical order ( LOW ahead of MED ).

How can I do this so that I can order ticket_priority_id , but not in alphabetical order, but in importance. I want the order (top to bottom) ASAP - HIGH - MED - LOW

+4
source share
2 answers

You can use the case statement in your order as shown

 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 
+8
source

You can use FIELD :

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

Please note that if ticket_priority_id not in the following list, it will return 0, so you might want to add this as well:

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

put lines that have no priority at the bottom, and not at the top.

+5
source

All Articles