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

被折叠的 条评论
为什么被折叠?



