我还没试过,但我想先问问专家。是否可以查询order by子句的一部分是2列的乘法?
我的意思是:
SELECT * FROM table WHERE column1 = "some condition" ORDER BY date_added DESC, (column2 * column3) ASC, another_order DESC"
我需要根据2列相乘的答案来订购我的结果。
干杯!
答案 0 :(得分:0)
CREATE TABLE `multiorder`
(`id` int, `num1` int, `num2` int)
;
INSERT INTO `multiorder`
(`id`, `num1`, `num2`)
VALUES
(1, 5, 9),
(2, 9, 3),
(3, 4, 6),
(4, 9, 1),
(5, 4, 2),
(6, 5, 6)
;
对于SQL
SELECT *, (`num1` * `num2`) FROM `multiorder`
ORDER BY `num1` ASC, (`num1` * `num2`) ASC
输出
+----+------+------+-------------------+
| ID | NUM1 | NUM2 | (`NUM1` * `NUM2`) |
+----+------+------+-------------------+
| 5 | 4 | 2 | 8 |
| 3 | 4 | 6 | 24 |
| 6 | 5 | 6 | 30 |
| 1 | 5 | 9 | 45 |
| 4 | 9 | 1 | 9 |
| 2 | 9 | 3 | 27 |
+----+------+------+-------------------+