如果合并两个表的列

zpcoder 2009-01-12 03:54:54
--数据如下
create table #a([A] int,[B] int)
go
insert into #a
SELECT 1,2 UNION
SELECT 2,6 UNION
SELECT 3,5 UNION
SELECT 4,3 UNION
SELECT 5,4

select * from #a
select * from #a order by A desc

drop table #a

--如何得到下表 (就是把第二个表里的内容,直接拼到第一个表的后面。)
A B A2 B2
------------------------------
1 2 5 4
2 6 4 3
3 5 3 5
4 3 2 6
5 4 1 2
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bennyyyyy 2009-01-12
  • 打赏
  • 举报
回复
union
PrewinZZZ 2009-01-12
  • 打赏
  • 举报
回复
[sql]
select * from #a
Union all
select * from #a order by A desc
[\sql]
可以放下面,放后面就不知道了,或许需要建一个空表吧,类型要匹配
csdyyr 2009-01-12
  • 打赏
  • 举报
回复
create table #a([A] int,[B] int)
go
insert into #a
SELECT 1,2 UNION
SELECT 2,6 UNION
SELECT 3,5 UNION
SELECT 4,3 UNION
SELECT 5,4

select *,id=identity(int,1,1) into # from #a

select *,id=identity(int,1,1) into #t from #a order by A desc

select a.a,a.b,b.a as a2,b.b as b2
from # as a full join #t as b on a.id=b.id

drop table #a
drop table #
drop table #t
/*
a b a2 b2
----------- ----------- ----------- -----------
1 2 5 4
2 6 4 3
3 5 3 5
4 3 2 6
5 4 1 2
*/
csdyyr 2009-01-12
  • 打赏
  • 举报
回复
union all

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧