Home > Back-end >  How can i turn order by dynamic?
How can i turn order by dynamic?

Time:12-07

I have tried to order this sql with a conditional if

  FROM sgm_whatsapp
  WHERE `phone_number` = '51996268989' 
  IF(delivery_date > answer_date, ORDER BY delivery_date DESC, ORDER BY answer_date DESC);

It isn't working. Can someone help me?

CodePudding user response:

Order by IF

    FROM sgm_whatsapp
      WHERE `phone_number` = '51996268989' 
      order by       
   IF(delivery_date > answer_date, delivery_date , answer_date) desc;

CodePudding user response:

SELECT fields
FROM sgm_whatsapp
  WHERE `phone_number` = '51996268989' 
ORDER BY
CASE WHEN delivery_date > answer_date THEN delivery_date
ELSE answer_date 
END DESC
  • Related