您的位置:首页 > 数据库

sql中一列拆成两列

2017-05-11 21:13 246 查看
declare @table table (name nvarchar(4))
insert into @table
select '张三' union all
select '李四' union all
select '王五' union all
select '刘三' union all
select '杨二' union all
select '胡八' union all
select '赵六'

--方法1:
create table #t (id int identity(1,1),name nvarchar(4))
insert into #t(name) select * from @table

select a.name,b.name from #t a left join #t b on a.id=b.id-1 where a.id%2<>0

drop table #t

--方法2:
select identity(int, 1,1) AS id ,name
into #tt
from @table

select a.name, b.name
from #tt a left join #tt b on a.id = b.id-1
where a.id %2 <> 0

drop table #tt

其实是同一个方法.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: