尽管 2*3-5 = 1
为什么这两个查询有不同的输出?
SELECT * FROM table ORDER BY 1 asc, column2 desc;
对比
SELECT * FROM table ORDER BY 2*3-5 asc, column2 desc;
最佳答案
按结果集中的第一列排序,然后按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;
ORDER BY { column-Name | ColumnPosition | Expression }
ColumnPosition
An integer that identifies the number of the column in the SelectItems in the underlying query of the SELECT statement. ColumnPosition must be greater than 0 and not greater than the number of columns in the result table. In other words, if you want to order by a column, that column must be specified in the SELECT list.
请记住,在某些 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.
关于sql - 为什么以下查询有不同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34470827/