即使2*3-5 = 1
为什么这两个查询都有不同的输出?
SELECT * FROM table ORDER BY 1 asc, column2 desc;
VS
SELECT * FROM table ORDER BY 2*3-5 asc, column2 desc;
答案 0 :(得分:2)
按结果集中的第一列排序,然后按column2
SELECT * FROM table ORDER BY 1 asc, column2 desc;
按常量表达式排序column2
:
SELECT * FROM table ORDER BY 2*3-5 asc, column2 desc;
<=>
SELECT * FROM table ORDER BY column2 desc;
的 SqlFiddleDemo
强>
ORDER BY { column-Name | ColumnPosition | Expression }
ColumnPosition
一个整数,用于标识SELECT语句的基础查询中SelectItems中列的编号。 ColumnPosition必须大于0且不大于数字 结果表中的列数。换句话说,如果你想订购 按列,必须在SELECT列表中指定该列。
请记住,在某些RDBMS中,不允许像SQL Server
那样按常量排序:
SELECT *
FROM tab
ORDER BY 2*3-5 asc, column2 desc;
-- A constant expression was encountered in the ORDER BY list, position 1.