日期:2014-05-17  浏览次数:20345 次

Sql 语句关于 insert 表内已有数据
有3个表, 表1中 uid 为主键  表2,表3, uid 为 外键。 表2,表3 主键为id,

如下图
uid, uname, udate, usalary
1, zhangsan, 12-12-1985, 2000
2, lisi, 12-12-1989, 3000
3, wangwu, 12-12-1979, 5000

表2
id,uid, position, phone
1,1, manager, 123
2,2, worker, 321
3,3, worker, 321
4,3, officer, 321
5,3, manager, 321

表3
id,uid, address, code
1,1, 12 place, 10
2,2, 14 place, 10
3,3, 16 place, 20
4,3, 20 place, 30

现在我要加入一个 uid为4的到表1, 这个新的uid4的内容 要复制uid3的内容, 完成如下图:


如下图
uid, uname, udate, usalary
1, zhangsan, 12-12-1985,2000
2, lisi, 12-12-1989,3000
3, wangwu, 12-12-1979,5000
4, dingyi, 12-12-1979,5000

表2
id,uid, position, phone
1,1, manager, 123
2,2, worker, 321
3,3, worker, 321
4,3, officer, 321
5,3, manager, 321
6,4, worker, 321
7,4, officer, 321
8,4, manager, 321

表3
id,uid, address, code
1,1, 12 place, 10
2,2, 14 place, 10
3,3, 16 place, 20
4,3, 20 place, 30
5,4, 16 place, 20
6,4, 20 place, 30

我想问的是,有没有一个简单的语句, 我可以做到这样,

比如
表2,表3 的多条数据应该如何写?

------最佳解决方案--------------------
--需要写三个insert  没有什么捷径
--下面语句供参考(假设uid 和id 都是自增)
insert into 表1 select 'dingyi',udate, usalary from 表1 where uid=3
insert into 表2 select a.uid,position, phone from 表1 a,表2 b where a.uid=b.uid and a.uid=3
insert into 表3 select a.uid,address, code from 表1 a,表3 b where a.uid=b.uid and a.uid=3

------其他解决方案--------------------
INSERT INTO 表一 VALUES(4, 'dingyi', '12-12-1979',5000)

INSERT INTO 表2(uid,POSITION,phone)
SELECT 4,POSITION,phone
FROM 表2
WHERE [uid]=3

INSERT INTO 表3(uid, address, code)
SELECT 4,address, code
FROM 表3
WHERE [uid]=3

------其他解决方案--------------------
引用:
SQL code?



1234567891011

INSERT INTO 表一 VALUES(4, 'dingyi', '12-12-1979',5000)   INSERT INTO 表2(uid,POSITION,phone) SELECT 4,POSITION,phone FROM 表2 WHERE [uid]=3   INSERT INTO 表3(uid, address……

+1