SQL 如何把一张表的数据拆成几部分放在一张表里面

本文介绍如何使用SQL进行数据表的创建、批量数据插入,并通过多表关联查询来展示复杂的数据关系。具体包括临时表的创建、数据批量插入方法及利用ROW_NUMBER()函数进行有序的多表连接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

create table #tmp
(
xh1 int null,
xm1 varchar(200) null,
xh2 int null,
xm2 varchar(200) null,
xh3 int null,
xm3 varchar(200) null,
xh4 int null,
xm4 varchar(200) null
)


create table #tmp1(
ID  int identity(1,1),
xh int,
xm varchar(200) null
)


insert into #tmp1
select 1011,'test1'
union all
select 1121,'test2'
union all
select 1131,'test3'
union all
select 1141,'test4'
union all
select 1151,'test5'
union all
select 1161,'test6'
union all
select 1171,'test7'
union all
select 1181,'test8'
union all
select 1191,'test9'
union all
select 1110,'test10'
union all
select 1111,'test11'
union all
select 1112,'test12'
union all
select 1113,'test13'
union all
select 1114,'test14'
union all
select 1115,'test15'




select a.*,b.*,c.*,d.*
from (select row_number() OVER(ORDER BY id) as [IDX],* from #tmp1 where id >=1 and id <=4) a
left join (select row_number() OVER(ORDER BY id) as [IDX],* from #tmp1 where id >= 5 and id <=8) b on a.[IDX] = b.[IDX]
left join (select row_number() OVER(ORDER BY id) as [IDX],* from #tmp1 where id >=9 and ID <=12) c on a.[IDX] = c.[IDX]
left join (select row_number() OVER(ORDER BY id) as [IDX],* from #tmp1 where id >=13 and ID <= 16) d on a.[IDX] = d.[IDX]



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值