0
votes

There's complex mysqli query which returns an array of products. I need to sort the result in a specific way: the products must be sorted by price (asc or desc), and then all the products with quantity=0 must be placed in the end of the result array. So the returned array is:
product_id      name     price      quantity
0001             product1   100          3
0005             product2   105          2
0003             product3   110         10
0002             product4   115          4
0008             product5   120          2
0004             product6   105          0
0005             product2   135          0

Doing ORDER BY quantity DESC, price doesn't help because all products become ordered by quantity before being ordered by price. Otherwise, ordering BY price, quantity DESC places all the products with quantity=0 in the middle of the table. Need some help.

1

1 Answers

3
votes

Use this:

ORDER BY quantity = 0, price

quantity = 0 will be 1 for all the items with zero quanity, it will be 0 for everything else.