当数据来自多个选择并将其合并在一起时,是否可以订购?如
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
但这不起作用。
写吧
Select id,name,age
From Student
Where age < 15
Union
Select id,name,age
From Student
Where Name like "%a%"
Order by name
order by 应用于完整的结果集
Select id,name,age
from
(
Select id,name,age
From Student
Where age < 15
Union
Select id,name,age
From Student
Where Name like "%a%"
) results
order by name
xxx UNION yyy ORDER BY zzz
等同于 (xxx UNION yyy) ORDER BY zzz
为了使排序仅适用于 UNION
中的第一条语句,您可以将其放在带有 UNION ALL
的子选择中(这两个在 Oracle 中似乎都是必需的):
Select id,name,age FROM
(
Select id,name,age
From Student
Where age < 15
Order by name
)
UNION ALL
Select id,name,age
From Student
Where Name like "%a%"
或者(针对 Nicholas Carey 的评论)您可以保证顶部 SELECT
是有序的,结果显示在底部 SELECT
上方,如下所示:
Select id,name,age, 1 as rowOrder
From Student
Where age < 15
UNION
Select id,name,age, 2 as rowOrder
From Student
Where Name like "%a%"
Order by rowOrder, name
select
语句的结果进行排序。根据 SQL 标准,结果的顺序是未定义的,除非有明确的 order by
子句。您的示例中的第一个 select
可能按子选择返回的顺序返回其结果,但不能保证。此外,*not* 保证整个 union
的结果集的排序(标准中的相同规则)。如果你依赖订单,你最终会被咬。
SELECT
内的顺序。
正如其他答案所述,last UNION
之后的 ORDER BY
应适用于通过联合连接的两个数据集。
我有两个数据集使用不同的表但相同的列。最后一个 UNION
之后的 ORDER BY
仍然无效。
为 ORDER BY
子句中使用的列使用别名就可以了。
SELECT Name, Address FROM Employee
UNION
SELECT Customer_Name, Address FROM Customer
ORDER BY customer_name; --Won't work
解决方案是使用别名 User_Name
,如下所示:
SELECT Name AS User_Name, Address FROM Employee
UNION
SELECT Customer_Name AS User_Name, Address FROM Customer
ORDER BY User_Name;
mismatched input 'FOR' expecting <EOF>
其他两个答案都是正确的,但我认为值得注意的是,我被卡住的地方没有意识到您需要按别名排序并确保两个选择的别名相同......所以
select 'foo'
union
select item as `foo`
from myTable
order by `foo`
请注意,我在第一个选择中使用单引号,而在其他选择中使用反引号。
这将为您提供所需的排序。
SELECT a, b, c FROM (<insert union query here>) AS x;
中来解决这个问题。
Order By
在 union
之后应用,因此只需在语句末尾添加 order by
子句:
Select id,name,age
From Student
Where age < 15
Union
Select id,name,age
From Student
Where Name like '%a%'
Order By name
如果我希望排序仅应用于 UNION
之一(如果使用 UNION ALL
):
Select id,name,age
From Student
Where age < 15
Union all
Select id,name,age
From
(
Select id,name,age
From Student
Where Name like "%a%"
Order by name
)
为了添加到一个旧主题,我使用了 ROW_NUMBER(使用 MS SQL)。这允许在 UNION 中进行排序(排序)。因此,使用来自 @BATabNabber 的想法来分隔联合的每一半,并将整个事物包装在一个选择中的 @Wodin,我得到:
Select Id, Name, Age from
(
Select Id, Name, Age, 1 as Mainsort
, ROW_NUMBER() over (order by age) as RowNumber
From Student
Where Age < 15
Union
Select Id, Name, Age, 2 as Mainsort
, ROW_NUMBER() over (Order by Name) as RowNumber
From Student
Where Name like '%a%'
) as x
Order by Mainsort, RowNumber
因此,调整或省略您想要排序的内容,并根据需要添加降序。
向查询中添加一列,该列可以子标识要对其进行排序的数据。
在下面的示例中,我使用了一个 Common Table Expression 和 选择您显示的内容,并将它们放入 CTE 上的特定组中;然后将这两个组中的 union
放入 AllStudents
。
然后,最终选择将首先对 SortIndex
列进行 AllStudents
排序,然后然后按 name
进行排序,例如:
WITH Juveniles as
(
Select 1 as [SortIndex], id,name,age From Student
Where age < 15
),
AStudents as
(
Select 2 as [SortIndex], id,name,age From Student
Where Name like "%a%"
),
AllStudents as
(
select * from Juveniles
union
select * from AStudents
)
select * from AllStudents
sort by [SortIndex], name;
总而言之,它将首先按组排序所有学生,然后按组内的名称进行子排序。
要将 ORDER BY 或 LIMIT 子句应用于单个 SELECT,请将 SELECT 括起来并将子句放在括号内:
(SELECT a FROM t1 WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2 ORDER BY a LIMIT 10);
可以使用这个:
Select id,name,age
From Student
Where age < 15
Union ALL
SELECT * FROM (Select id,name,age
From Student
Where Name like "%a%")
为什么不使用TOP X?
SELECT pass1.* FROM
(SELECT TOP 2000000 tblA.ID, tblA.CustomerName
FROM TABLE_A AS tblA ORDER BY 2) AS pass1
UNION ALL
SELECT pass2.* FROM
(SELECT TOP 2000000 tblB.ID, tblB.CustomerName
FROM TABLE_B AS tblB ORDER BY 2) AS pass2
TOP 2000000 是一个任意数字,足以捕获所有数据。根据您的要求进行调整。
不定期副业成功案例分享
name
怎么办?您能否为ORDER BY
提供两个表的别名,但从结果集中省略它?