如何在 SQL 中使用 union 排序? [英] How to order by with union in SQL?

查看:25
本文介绍了如何在 SQL 中使用 union 排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数据来自多个选择并将其合并在一起时,是否可以进行排序?比如

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?

我试过了

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 应用于完整的结果集

the order by is applied to the complete resultset

这篇关于如何在 SQL 中使用 union 排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆