MYSQL按文字排序

时间:2015-10-03 14:20:03

标签: php mysql sorting

好的,所以我知道如何按ID或数字排序

$getTicket = $sql->query("SELECT * FROM `ticket` 
                          WHERE `user`='$user->name' 
                          ORDER BY `id` DESC");

我在票证表中有状态,并且我有三件事:

1)已回答2)未答复3)完成

我想以这种方式对其进行排序:

1)未答复
2)回答
3)完成

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:5)

简单如下 -

ORDER BY FIELD(Status, 'Unanswered', 'Answered', 'Done')

答案 1 :(得分:4)

您通常可以使用case

SELECT * FROM `ticket` 
WHERE `user` = '$user->name' 
ORDER BY case when Status = 'Unanswered' then 1
              when Status = 'Answered' then 2
              else 3
         end

或MySQL特定的find_in_set

SELECT * FROM `ticket` 
WHERE `user` = '$user->name' 
ORDER BY find_in_set(Status, 'Unanswered,Answered,Done')