0

i have a ticket table like this

tickets : id , user_id , title , has_answer(bool 0,1) , created_at 

i want to sort my tickets by new to old (id desc) but i want unanswered tickets at the top

so i can write somethign like

order by has_answer asc , id desc 

this way i get unanswered tickets at the top but they will also be sorted by id asc , i want older unanswered tickets to be shown at the top

basically i want unanswered ticket at top and sorted by id asc and rest of them (answered) at the bottom sorted by id desc

hretic
  • 999
  • 9
  • 36
  • 78
  • I think it will be useful in your case [stackoverflow](https://stackoverflow.com/questions/514943/order-a-mysql-table-by-two-columns). – Dailosrs Nov 03 '21 at 17:31

1 Answers1

0

If id is a numeric data type you can use conditional sorting with a CASE expression:

ORDER BY has_answer, 
         CASE WHEN has_answer THEN -id ELSE id END
forpas
  • 160,666
  • 10
  • 38
  • 76