如何与工会在SQL排序?

当数据来自许多选择和联合在一起时,是否有可能进行排序?如

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

但这并不奏效。

507148 次浏览
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

只写

Select id,name,age
From Student
Where age < 15
Union
Select id,name,age
From Student
Where Name like "%a%"
Order by name

顺序被应用到完整的结果集

其他两个答案都是正确的,但我认为值得注意的是,我陷入困境的地方是没有意识到你需要按别名排序,并确保两个选择的别名是相同的…所以

select 'foo'
union
select item as `foo`
from myTable
order by `foo`

请注意,我在第一个选择中使用单引号,而在其他选择中使用反引号。

这样就能得到你需要的排序。

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
)

为了使排序只适用于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

正如其他答案所述,ORDER BY最后的 UNION之后应该适用于由union连接的两个数据集。

我有两个使用不同表但相同列的数据集。ORDER BY在最后一个UNION之后仍然没有工作。

使用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;

可以用这个:

Select id,name,age
From Student
Where age < 15
Union ALL
SELECT * FROM (Select id,name,age
From Student
Where Name like "%a%")

要将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);


向查询中添加一个列,该列可以对要排序的数据进行子标识。

在下面的例子中,我使用了带有选择显示的内容的公共表表达式,并将它们放在CTE上的特定组中;然后从这两个组中执行unionAllStudents

最后的选择将首先对AllStudents 通过列进行排序,然后对然后列进行排序,例如:

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;

总而言之,它将获得所有的学生,这些学生将首先按组排序,然后按组内的名字进行子排序。

为什么不用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是一个任意的数字,它大到足以捕获所有的数据。根据您的要求进行调整。

添加到一个旧的主题,我使用ROW_NUMBER(使用MS SQL)。这允许在union内排序(顺序)。 因此,使用@BATabNabber中分离Union的每一半的思想,以及@Wodin中将整个内容包装在select中,我得到:

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

所以调整,或省略,你想要的顺序,并添加下降,因为你认为合适。