Answer the question
In order to leave comments, you need to log in
How to sort by two fields as if it were one field?
The initial table table consists of two fields: list_price - the price of the product before the discount, sell_price - the price of the product, taking into account the discount.
+------------+-------------+
| list_price | sell_price |
+------------+-------------+
| 8 | 4 |
| 10 | 5 |
| 7 | 3 |
| 0 | 12 |
| 0 | 42 |
+------------+-------------+
+------------+-------------+
| list_price | sell_price |
+------------+-------------+
| 0 | 42 |
| 0 | 12 |
| 10 | 5 |
| 8 | 4 |
| 7 | 3 |
+------------+-------------+
+------------+-------------+
| list_price | sell_price |
+------------+-------------+
| 10 | 5 |
| 8 | 4 |
| 7 | 3 |
| 0 | 42 |
| 0 | 12 |
+------------+-------------+
Answer the question
In order to leave comments, you need to log in
IF() will help:
SELECT
list_price,
sell_price,
IF( list_price=0, sell_price, list_price) AS orderme
FROM `_tmp_test`
ORDER BY orderme DESC
The question is not entirely clear.
From what I understand, I can offer 2 options (sorry, but presented "as it should" does not explain how to sort):
1) SELECT list_price,sell_price FROM table ORDER BY sell_price DESC;
2) SELECT list_price,sell_price FROM table where list_price = '0' ORDER BY sell_price DESC
union
SELECT list_price,sell_price FROM table where list_price <> '0' ORDER BY sell_price DESC
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question