Gautam Jha
Gautam Jha

Reputation: 1473

SQL order by attribute in column

i want to order some record by latest date then where status=wait then status=cancel and in last status=enrolled.

SqlDatabase

    #   Name    Type    Collation   Attributes  Null    Default
     1  id  int(11)  No None    AUTO_INCREMENT  Change Change   Drop Drop   

     2  name    varchar(100)    latin1_swedish_ci       No  None        



    3   status  varchar(256)    latin1_swedish_ci       No  wait    


   4    date    datetime            No  CURRENT_TIMESTAMP   

currently i am using this query but

SELECT * FROM demo ORDER BY date desc,status='wait' desc,id desc,status='enrolled' asc,status='cancel' asc LIMIT 10

this query not giving me the expected result, please help me out.

Upvotes: 1

Views: 1084

Answers (1)

sagi
sagi

Reputation: 40481

You mixed the order of them a little bit , this should order by the logic you provided in the question.

SELECT * FROM demo
ORDER BY date desc,
         status='wait' desc,
         status='enrolled' desc,
         status='cancel' desc,
         id desc 
LIMIT 10

Upvotes: 1

Related Questions