I came across a query today,
我今天遇到一个问题,
SELECT col1,'yes' as col2 FROM myTable
WHERE col2=TRUE
UNION
SELECT col1,'no' as col2 FROM mytable
WHERE col2=FALSE
ORDER BY 1,2
I thought it would order by first column and then second but since a UNION is involved I am a bit unsure can someone explain the exact meaning of this query
我原以为它会按第一列和第二列排序,但由于涉及到UNION,我有点不确定是否有人能解释这个查询的确切含义
1 个解决方案
#1
13
SQL Server will union the results together (which is an implied select distinct) and then order the results by col1 then col2. In a union query, you can put an ORDER BY on the final select, which will sort the final result.
SQL Server将把结果合并在一起(这是一个隐含的select distinct),然后通过col1然后col2对结果进行排序。在union查询中,您可以在final select上放置一个ORDER BY,它将对最终结果进行排序。
#1
13
SQL Server will union the results together (which is an implied select distinct) and then order the results by col1 then col2. In a union query, you can put an ORDER BY on the final select, which will sort the final result.
SQL Server将把结果合并在一起(这是一个隐含的select distinct),然后通过col1然后col2对结果进行排序。在union查询中,您可以在final select上放置一个ORDER BY,它将对最终结果进行排序。