在sql 2005中的union + order by [英] union +order by in sql 2005

查看:90
本文介绍了在sql 2005中的union + order by的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将所有两个查询与具有order by子句的第二个查询结合起来。

例子:



选择id,表中的名字1

联合所有

选择id,从表2中按名称命名。



在我的预期结果中只订购第二个查询结果,而不是整个结果。

i want to union all two queries with second query having order by clause.
examp:

select id,name from table 1
union all
select id,name from table 2 order by name.

in my expected result order the second query result only,not the entire result.

推荐答案



试试这个....

Hi
Try This....
select id,name from table_1
union all
SELECT T.ID,T.name 
FROM (select TOP 100 PERCENT id,name from table_2 order by name) T





检查以下链接...

使用派生表简化SQL Server查询过程 [ ^ ]

问候,

GVPrabu



Check the following link also...
Using Derived Tables to Simplify the SQL Server Query Process[^]
Regards,
GVPrabu


hi,



使用以下查询









use the following query



create table #temp (ID int, name varchar(50))

Insert into #temp   select id,name  from table1
Insert into #temp   select id,name  from table2 order by ID desc

select * from #temp
drop table #temp







它可以帮到你..



问候,

Prakash.T




it may help you..

Regards,
Prakash.T


这篇关于在sql 2005中的union + order by的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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