Greenway
Greenway

Reputation: 1

Mysql - multiple Order By options in CASE WHEN

I'm pulling results from a table in SQL to generate a 'next' button link in our appointment ordering system. The SELECT statement works, but I want to do something like this with the order by to account for situations where two appointments take place at the exact same time and date:

ORDER BY
     CASE Order.date
          WHEN currentDate THEN (Order.id ASC, Order.Date ASC)
          ELSE (Order.Date ASC, Order.id ASC)
          END

Upvotes: 0

Views: 88

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

You could write it as

ORDER BY
     CASE WHEN Order.date = current_date() THEN 0 ELSE 1 END ASC,
     Order.Date ASC, 
     Order.id ASC

It will first sort rows where Order.date is today then it will sort by date in ascending order and for same dates it will sort rows by id in ascending order

Upvotes: 1

Related Questions