日期:2014-05-18  浏览次数:20436 次

INSERT两个数据,分别来自2张表
table1(zd1,zd2)
table2(tt,zz)
table3(kk,dd)
我要把table2中的zz 和table3中的dd 插入到table1(zd1,zd2)中
sql语句该怎么写?

------解决方案--------------------
SQL code

insert table1(zd1,zd2)
select zz,dd from table2 a,table3 b
where  a.关键字段=b.关键字段

------解决方案--------------------
探讨
引用:
SQL code


insert table1(zd1,zd2)
select zz,dd from table2 a,table3 b
where a.关键字段=b.关键字段


table2和table3 没关系

------解决方案--------------------
Table2和Table3有啥关联关系啊?
insert table1(zd1,zd2)
select zz,dd 
from table2 a,table3 b
where a.关键字段=b.关键字段
------解决方案--------------------
SQL code

insert table1(zd1,zd2)
select zz,dd from(
select px=row_number()over(order by getdate()),zz from table2)a
full join (
select px=row_number()over(order by getdate()),dd from table3)b
on a.px=b.px

------解决方案--------------------
insert into table1(zd1)
select zz from table2

insert into table1(zd2)
select dd from table3
------解决方案--------------------
update table1 set zd1=(SELECT zz FROM table2 WHERE tt='asd')
update table1 set zd2 = (SELECT dd FROM table3 WHERE kk='jkl')
------解决方案--------------------
路过,这三张表应该有关联的。

就用这条语句:

SQL code

INSERT INTO table1(zd1,zd2) SELECT zz,dd FROM table2 a , table3 b WHERE a.tt='asd' and b.kk='jkl'