Mysql命令(column1 * column2)

时间:2013-01-23 09:54:10

标签: mysql

我还没试过,但我想先问问专家。是否可以查询order by子句的一部分是2列的乘法?

我的意思是:

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

我需要根据2列相乘的答案来订购我的结果。

干杯!

1 个答案:

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

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