表字段如下:
id name tel t_id t_name t_tel
001 张三 13960717119 002 李四 13766912345
002 李四 13766912423 001 张三 13960717219
003
004.........要得要如下结果
id name tel t_id t_name t_tel
001 张三 13960717119 002 李四 13766912423
002 李四 13766912423 001 张三 13960717119
003
003
.......
id name tel t_id t_name t_tel
001 张三 13960717119 002 李四 13766912345
002 李四 13766912423 001 张三 13960717219
003
004.........要得要如下结果
id name tel t_id t_name t_tel
001 张三 13960717119 002 李四 13766912423
002 李四 13766912423 001 张三 13960717119
003
003
.......
set tb.t_tel=t.tel
from tb t
where tb.t_id=t.id
-- try
update a set t_tel=b.tel
from tablename a inner join tablename b on a.t_id=b.id
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] varchar(3),[name] varchar(4),[tel] bigint,
[t_id] varchar(3),[t_name] varchar(4),[t_tel] bigint)
insert [tb]
select '001','张三',13960717119,'002','李四',13766912345 union all
select '002','李四',13766912423,'001','张三',13960717219update a
set a.t_tel=b.tel
from tb a,tb b
where a.t_id=b.id
select * from [tb]
--测试结果:
/*
id name tel t_id t_name t_tel
---- ---- -------------------- ---- ------ --------------------
001 张三 13960717119 002 李四 13766912423
002 李四 13766912423 001 张三 13960717119(所影响的行数为 2 行)
*/
update b set t_tel=a.tel
from b,a
where a.t_id=b.id