Mysql- How to sort the rows based on 2 columns in an irregular order?

baig772 :

I have a table with below structure

id| name | read | status   | is_assigned | created_at
1 | ABC  | 0    | ordered  | 0           | 2020-03-27 11:48:38
2 | XYZ  | 1    | accepted | 1           | 2020-03-27 11:48:38
3 | DEF  | 0    | ordered  | 0           | 2020-03-25 11:48:38
4 | JKL  | 1    | accepted | 0           | 2020-03-16 11:48:38
5 | GHI  | 1    | on-hold  | 1           | 2020-03-23 11:48:38

I want to priorities the sorting order as below :

  1. Where read = 0
  2. Where Status = ordered
  3. Where status = accepted but is_assigned = 0
  4. Sort By created_at DESC

The query I have is only valid for created_at and read columns i.e.

select * from orders order by read ASC, created_at DESC

How can I achieve the above sorting order for the described table and map this query in Laravel Query Builder

TIA

GMB :

You can translate directly your logic in the order by clause, using MySQL's ability to evaluate conditions are 0/1 values:

order by
    (read = 0) desc,
    (status = 'ordered') desc,
    (status = 'accepted' and is_assigned = 0) desc,
    created_at

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360115&siteId=1