Sql服务器联合但保持秩序 [英] Sql server union but keep order

查看:27
本文介绍了Sql服务器联合但保持秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法联合两个表,但让第一个表中的行首先出现在结果集中?

Is there a way to union two tables, but keep the rows from the first table appearing first in the result set?

例如:

表 1

name        surname
-------------------
John         Doe
Bob          Marley
Ras          Tafari

表 2

name       surname
------------------
Lucky      Dube
Abby       Arnold

我希望结果集是:

name        surname
-------------------
John         Doe
Bob          Marley 
Ras          Tafari
Lucky      Dube
Abby       Arnold

不幸的是,union 以某种方式重新排列了表格.有没有办法解决这个问题?

Unfortunately, union somehow reorders the table. Is there a way around this?

推荐答案

试试这个 :-

Select * 
from
( 
Select name,surname, 1 as filter
from  Table1
Union all
Select name,surname , 2 as filter
from Table2
)
order by filter

这篇关于Sql服务器联合但保持秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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