2015-10-03 62 views
3

好了,所以我知道我可以通過ID或數字類似mysql命令

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

我有票表狀況進行排序,並在這我有三樣東西:

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

我想將它以這種方式進行排序:

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

有沒有辦法做到這一點?

回答

5

做簡單的原樣

ORDER BY FIELD(Status, 'Unanswered', 'Answered', 'Done') 
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')