日期:2014-05-18  浏览次数:20716 次

多个表的合并后(union all),如何获得前 10 条记录???
比如如下两个表合并后,我如何能用得到钱10条语句:
select 姓名=aname,支出钱=amoney,收入钱='',时间=atime from a union all select bname,支出钱='',收入钱=bmoney,时间=btime from b order by 时间

声明我希望一句话就能搞定,不想用存储过程`~~~

------解决方案--------------------
select 姓名=aname,支出钱=amoney,收入钱='',时间=atime from a 
inner join 
(select top 10 [time]
from
(select 
atime as [time] from a 
union 
select btime as [time] from b order by [time]) c)
d on a.atime = d.[time]

union
select bname,支出钱='',收入钱=bmoney,时间=btime from b 
inner join 
(select top 10 [time]
from
(select 
atime as [time] from a 
union 
select btime as [time] from b order by [time]) c)
d on b.btime = d.[time]
------解决方案--------------------
SQL code

SELECT TOP 10 * FROM (select 姓名=aname,支出钱=amoney,收入钱='',时间=atime from a union all select bname,支出钱='',收入钱=bmoney,时间=btime from b order by 时间 ) as temp

------解决方案--------------------
楼上的应该可以
------解决方案--------------------
select top 10 * from 
(select 姓名=aname,支出钱=amoney,收入钱='',时间=atime from a union all select bname,支出钱='',收入钱=bmoney,时间=btime from b order by 时间 
) as a
------解决方案--------------------
select top 10 * from (select 姓名=aname,支出钱=amoney,收入钱='',时间=atime from a union all select bname,支出钱='',收入钱=bmoney,时间=btime from b )
as AA
order by 时间