首页 > 代码库 > sql中一列拆成两列

sql中一列拆成两列

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

其实是同一个方法.

sql中一列拆成两列