0

I would like use CASE statement in ORDER BY clausule like this:

ORDER BY  
    CASE
        WHEN `Delivery`.`type` = 0 THEN `Delivery`.`quantity` ASC
        WHEN `Delivery`.`type` = 1 THEN `Delivery`.`quantity` DESC
    END

But I can use ASC/DESC keywords only after the END.

There is some solution for this?

kicaj
  • 2,881
  • 5
  • 42
  • 68

2 Answers2

0

You can use query like:

SELECT columns FROM tables WHERE condition 
ORDER BY  
CASE    
  WHEN `Delivery`.`type` = 0 THEN `Delivery`.`quantity` ASC,
  WHEN `Delivery`.`type` = 1 THEN `Delivery`.`quantity` DESC
END;
Aman Aggarwal
  • 17,619
  • 9
  • 53
  • 81
0

Using the following link for reference, I've put this together for your scenario.

MySQL conditional ORDER BY ASC/DESC for date column

ORDER BY 
      CASE WHEN `Delivery`.`type` = 0 THEN `Delivery`.`quantity` END ASC,
      CASE WHEN `Delivery`.`type` = 1 THEN `Delivery`.`quantity` END DESC;
Community
  • 1
  • 1
Nathan
  • 160
  • 7