MySQL:按多列排序结果
假设我有下表“产品”:MySQL:按多列排序结果,mysql,Mysql,假设我有下表“产品”: +----+------------+------------+----------+------+ | ID | Name | Purchased | Promoted | Used | +----+------------+------------+----------+------+ | 0 | TV | 2017-10-02 | 0 | 0 | | 1 | Radio | 2017-08-27 | 1
+----+------------+------------+----------+------+
| ID | Name | Purchased | Promoted | Used |
+----+------------+------------+----------+------+
| 0 | TV | 2017-10-02 | 0 | 0 |
| 1 | Radio | 2017-08-27 | 1 | 0 |
| 2 | Fridge | 2017-09-03 | 1 | 1 |
| 3 | Dishwasher | 2017-08-15 | 0 | 1 |
| 4 | Bike | 2017-09-17 | 0 | 1 |
| 5 | Computer | 2017-10-03 | 0 | 0 |
+----+------------+------------+----------+------+
现在,我想按以下顺序获取所有记录:
+---+------------+------------+---+---+
| 2 | Fridge | 2017-09-03 | 1 | 1 |
+---+------------+------------+---+---+
| 1 | Radio | 2017-08-27 | 1 | 0 |
| 4 | Bike | 2017-09-17 | 0 | 1 |
| 3 | Dishwasher | 2017-08-15 | 0 | 1 |
| 5 | Computer | 2017-10-03 | 0 | 0 |
| 0 | TV | 2017-10-02 | 0 | 0 |
+---+------------+------------+---+---+
MySQL查询是什么?看起来很简单:
ORDER BY Promoted = 1 DESC,
Used = 1 DESC,
Purchased DESC
MySQL将布尔表达式计算为1和0。您可以使用UNION语句,如
SELECT * FROM products WHERE Promoted='1' ORDER BY Purchased
UNION
SELECT * FROM products WHERE Promoted!='1' AND Used='1' ORDER BY Purchased
UNION
SELECT * FROM products WHERE Promoted!='1' AND Used!='1' ORDER BY Purchased
从产品订单中按PRODUCT.PROMOTED DESC、PRODUCT.USED DESC选择*
我看不到你的尝试?!!?