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

求一sql语句,急,解决了就给分,谢谢各位大侠!
一个表   t1
字段名       a,b,c

表2     t2
字段名       a,b

怎样用一条sql实现   把t1中字段a存在与t2中的所有记录的c标记为1,并同步t2的b字段到t1表的b字段呢?

例如  
t1   表           a         b         c
                    1    
                    2
                    3

t2   表           a         b
                    1         1
                    2         2

我要得到的结果是更新   t1   表为
                    a         b         c
                    1         1         1
                    2         2         1
                    3

------解决方案--------------------
update t1
set b = t2.b,
c = 1
from t1,t2
where t1.a = t2.a
------解决方案--------------------
update t1 from t2 a set b= t2.b where t1.a=t2.a
update t1 set c=1 where b is not null
select * from t1
------解决方案--------------------
create table t1(a int,b int ,c int)
insert into t1 values(1,null,null)
insert into t1 values(2,null,null)
insert into t1 values(3,null,null)
create table t2(a int,b int)
insert into t2 values(1,2)
insert into t2 values(2,2)
go

update t1
set b = t2.b,
c = 1
from t1,t2
where t1.a = t2.a

select * from t1

drop table t1,t2

/*
a b c
----------- ----------- -----------
1 2 1
2 2 1
3 NULL NULL

(所影响的行数为 3 行)
*/