CSDN – SQL Area 求一UPDATE 语句...

 

CSDN – SQL Area   求一UPDATE 语句...

 

 

 

A 字段 A1A2A3
          1  2  3
          1  4  8
          2  5  3
          2  6  8
。。。。。。。。。。。。。。

修改为
 
          1  2  3
          1  2  8
          2  5  3
          2  5  8
。。。。。。。。。。。。。。

A
表中A1字段相等两条记录 A3等于8的那一条的A2改为A3 等于3A2

 

 

 

--解答--

 

declare @t1 table(A1 varchar(4),

               A2 varchar(4),

               A3 varchar(4)

               )

 

 

insert into @t1

select             '1', '2','3'

union select    '1', '4','8'

union select    '2', '5','3'

union select    '2', '6','8'

 

 

--select * from @t1

 

/*A1 相同,取最小的A2 的值*/

 

--update b set b.a2 =(select min(a2) from @t1 where b.a1 = a1  ) from @t1 b

 

--select * from @t1

 

/*A 表中A1字段相等两条记录 A3等于8的那一条的A2改为A3 等于3A2 */

 

update b set b.a2=(select a2 from @t1 where b.a1 = a1 and A3 ='3' ) from @t1 b where b.a3 = '8'

 

select * from @t1

 

 

---Result---

 

A1   A2   A3  

---- ---- ----

1    2    3

1    2    8

2    5    3

2    5    8

 

posted on 2008-08-06 10:16  封起De日子  阅读(98)  评论(0编辑  收藏  举报

导航