为什么以下查询有不同的输出?
Why following queries have different output?
尽管 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.
尽管 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.