当前位置: 代码迷 >> Sql Server >> |M| 求两表进行插入多条记录SQL解决方法
  详细解决方案

|M| 求两表进行插入多条记录SQL解决方法

热度:66   发布时间:2016-04-27 19:34:22.0
|M| 求两表进行插入多条记录SQL
如有表User
ID User
1 A
2 B
3 C
4 D
5 E

表模块
ModuleID UserID
1 1
2 1
3 1
---------------------------
然后现在要将User ID >3 的用户插到模块表中和用户1模块相同 最后为
ModuleID UserID
1 1
2 1
3 1
1 4
2 4
3 4
1 5
2 5
3 5

Sql语句要怎么写 谢谢

------解决方案--------------------
顶一下哈
------解决方案--------------------
create table T1(ID int, [User] varchar(100))


insert into T1 select 1, 'A'
insert into T1 select 2, 'B' 
insert into T1 select 3, 'C'
insert into T1 select 4, 'D' 
insert into T1 select 5, 'E' 

create table T2(ModuleID int, UserID int)


insert into T2 select 1, 1 
insert into T2 select 2, 1 
insert into T2 select 3, 1 


insert into T2
select b.ModuleID,a.id
from (select * from T1 where ID>3) as a inner join (select * from T2 where UserID=1) as b on 1=1

select * from T2

drop table T1,T2


/*
--结果
ModuleID UserID
1 1
2 1
3 1
1 4
2 4
3 4
1 5
2 5
3 5



*/
  相关解决方案