-2

我还没有尝试过,但我想我会先问专家。是否可以进行查询,其中 order by 子句的一部分是 2 列的乘积?

我的意思是:

SELECT * FROM table WHERE column1 = "some condition" ORDER BY date_added DESC, (column2 * column3) ASC, another_order DESC"

我需要根据将 2 列相乘的结果来排序我的结果。

干杯!

4

1 回答 1

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 |
+----+------+------+-------------------+

小提琴:http ://www.sqlfiddle.com/#!2/b19a4/3

于 2013-01-23T10:02:12.900 回答