如何在 SQL 中使用 union 进行排序?
当数据来自多个 select 并将其联合在一起时,是否可以排序?例如
Select id,name,age
From Student
Where age < 15
Union
Select id,name,age
From Student
Where Name like "%a%"
如何按名称排序此查询?
我尝试过
Select id,name,age
From Student
Where age < 15 or name like "%a%"
Order by name
但这不起作用。
Is it possible to order when the data is come from many select and union it together? Such as
Select id,name,age
From Student
Where age < 15
Union
Select id,name,age
From Student
Where Name like "%a%"
How can I order this query by name?
I tried this
Select id,name,age
From Student
Where age < 15 or name like "%a%"
Order by name
But that does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
只需编写
order by 即可应用于完整的结果集
Just write
the order by is applied to the complete resultset
为了使排序仅应用于
UNION
中的第一个语句,您可以将其放入带有UNION ALL
的子查询中(这两者在 Oracle 中似乎都是必需的) ):或者(针对 Nicholas Carey 的评论)您可以保证顶部的 SELECT 是有序的,并且结果显示在底部的 SELECT 上方,如下所示:
In order to make the sort apply to only the first statement in the
UNION
, you can put it in a subselect withUNION ALL
(both of these appear to be necessary in Oracle):Or (addressing Nicholas Carey's comment) you can guarantee the top
SELECT
is ordered and results appear above the bottomSELECT
like this:正如其他答案所述,last
UNION
之后的ORDER BY
应适用于通过并集连接的两个数据集。我有两个数据集,使用不同的表但相同的列。最后一个
UNION
之后的ORDER BY
仍然不起作用。使用 ORDER BY 子句中使用的列的别名就可以达到目的。
解决方案是使用别名
User_Name
,如下所示:As other answers stated,
ORDER BY
after the lastUNION
should apply to both datasets joined by union.I had two datasets using different tables but the same columns.
ORDER BY
after the lastUNION
still didn't work.Using an alias for the column used in the
ORDER BY
clause did the trick.The solution was to use the alias
User_Name
, shown below:其他两个答案都是正确的,但我认为值得注意的是,我陷入困境的地方没有意识到您需要按别名进行排序,并确保两个选择的别名相同......所以
请注意我在第一个选择中使用单引号,但在其他选择中使用反引号。
这将为您提供所需的排序。
Both other answers are correct, but I thought it worth noting that the place where I got stuck was not realizing that you'll need order by the alias and make sure that the alias is the same for both the selects... so
notice that I'm using single quotes in the first select but backticks for the others.
That will get you the sorting you need.
Order By
在union
之后应用,所以只需在语句末尾添加一个
order by
子句:Order By
is applied afterunion
, so justadd an
order by
clause at the end of the statements:如果我希望排序仅应用于其中一个
UNION
(如果使用UNION ALL
):If I want the sort to be applied to only one of the
UNION
if useUNION ALL
:为了添加到旧主题,我使用了 ROW_NUMBER(使用 MS SQL)。这允许在 UNION 内进行排序(顺序)。
因此,使用 @BATabNabber 的想法来分隔联盟的每一半,并且 @Wodin 将整个内容包装在选择中,我得到:
所以调整或省略您想要排序的内容,并根据需要添加降序。
To add to an old topic, I used ROW_NUMBER (using MS SQL). This allows sorts (orders) within UNIONs.
So using an idea from @BATabNabber to separate each half of the Union, and @Wodin of wrapping the whole thing in a select, I got:
So adjust, or omit, what you want to Order by, and add Descendings as you see fit.
向查询添加一列,该列可以子标识要对其进行排序的数据。
在下面的示例中,我使用公共表表达式来选择您显示的内容并将它们放入 CTE 上的特定组中;然后将这两个组
联合
为AllStudents
。最终选择将首先按
SortIndex
列对AllStudents
进行排序,然后按name 进行排序code> 如:
总而言之,它将首先按组排序所有学生,然后按组内的名称进行子排序。
Add a column to the query which can sub identify the data to sort on that.
In the below example I use a Common Table Expression with the selects what you showed and places them into specific groups on the CTE; then do a
union
off of both of those groups intoAllStudents
.The final select will then sort
AllStudents
by theSortIndex
column first and then by thename
such as:To summarize, it will get all the students which will be sorted by group first, and subsorted by the name within the group after that.
要将 ORDER BY 或 LIMIT 子句应用于单个 SELECT,请将 SELECT 括起来并将子句放在括号内:
To apply an ORDER BY or LIMIT clause to an individual SELECT, parenthesize the SELECT and place the clause inside the parentheses:
可以用这个:
Can use this:
为什么不使用 TOP X?
TOP 2000000 是一个任意数字,足以捕获所有数据。根据您的要求进行调整。
Why not use TOP X?
The TOP 2000000 is an arbitrary number, that is big enough to capture all of the data. Adjust as per your requirements.