一个表 t1
字段名 a,b,c表2 t2
字段名 a,b怎样用一条sql实现 把t1中字段a存在与t2中的所有记录的c标记为1,并同步t2的b字段到t1表的b字段呢?例如
t1 表 a b c
1
2
3t2 表 a b
1 1
2 2我要得到的结果是更新 t1 表为
a b c
1 1 1
2 2 1
3
字段名 a,b,c表2 t2
字段名 a,b怎样用一条sql实现 把t1中字段a存在与t2中的所有记录的c标记为1,并同步t2的b字段到t1表的b字段呢?例如
t1 表 a b c
1
2
3t2 表 a b
1 1
2 2我要得到的结果是更新 t1 表为
a b c
1 1 1
2 2 1
3
set t1.b=(select t2.b from t2 where t1.a=t2.a),t1.c=1
where exists (select 1
from t2
where t1.a=t2.a)
from t2
where t1.a=t2.a)这个条件是什么意思呢?
SET T1.B = (SELECT T2.B FROM T2 WHERE T1.A = T2.A), T1.C = 1
WHERE T1.A IN (SELECT T2.A FROM T2);
2 decode(t1.a,t2.a,t2.b,null) as b,
3 decode(t1.a,t2.a,1,null) as c
4 from (select 1 as a,'' as b,'' as c from dual
5 union all
6 select 2 as a,'' as b,'' as c from dual
7 union all
8 select 3 as a,'' as b,'' as c from dual
9 )t1,
10 (select 1 as a,1 as b from dual
11 union all
12 select 2 as a,2 as b from dual
13 )t2
14 where t1.a = t2.a(+); A B C
---------- ---------- ----------
1 1 1
2 2 1
3