A表数据 name id state
张三 1 null
李四 2 null
王五 3 null
周六 4 null
B表数据 id
2
4
5
希望能通过update得到如下结果
name id state
张三 1 no
李四 2 yes
王五 3 no
周六 4 yes就是说如果A表中的ID再B表中能够找到,那么state为yes,如果不能则为no
张三 1 null
李四 2 null
王五 3 null
周六 4 null
B表数据 id
2
4
5
希望能通过update得到如下结果
name id state
张三 1 no
李四 2 yes
王五 3 no
周六 4 yes就是说如果A表中的ID再B表中能够找到,那么state为yes,如果不能则为no
where a.id = b.id(+)
from a, b
where a.id = b.id(+)
SQL> select * from a;
NAME ID STATE
-------------------- ---------- --------------------
张三 1
李四 2
王五 3
周六 4
SQL> select * from b;
ID
----------
2
4
5
SQL>
SQL> select a.name,a.id,decode(b.id,null,'no','yes') state
2 from a,b
3 where a.id = b.id(+)
4 order by a.id;
NAME ID STATE
-------------------- ---------- -----
张三 1 no
李四 2 yes
王五 3 no
周六 4 yes
SQL>
SQL> select a.name, a.id, nvl2(b.id, 'yes', 'no') as state
2 from a, b
3 where a.id = b.id(+)
4 order by a.id;
NAME ID STATE
-------------------- ---------- -----
张三 1 no
李四 2 yes
王五 3 no
周六 4 yes
from a, (select distinct id from b) b
where a.id = b.id(+)
select d.sp_id,d.serv_id,decode(c.serv_id,null,'2','1')
from
(select distinct serv_id,local_net_id,billing_cycle_id from ods.acct_item_owe@HNODSDB) c,
sp_tj subpartition(m200906_d0730) d
where d.serv_id=c.serv_id(+)
and c.local_net_id='730'
)其中c对应的是我开始说的B表,也就是配置表,总量有300多万条
d对应的是我开始说的A表,也就是数据表,总量11万条
select d.sp_id,d.serv_id,decode(c.serv_id,null,'2','1')
from
(select distinct serv_id from ods.acct_item_owe@HNODSDB) c,
sp_tj subpartition(m200906_d0730) d
where d.serv_id=c.serv_id(+)
and c.local_net_id='730'
)
会报错的
因为后面用到了and c.local_net_id='730'
select d.sp_id,d.serv_id,decode(c.serv_id,null,'2','1')
from
(select distinct serv_id from ods.acct_item_owe@HNODSDB where local_net_id='730') c,
sp_tj subpartition(m200906_d0730) d
where d.serv_id=c.serv_id(+))
when exists(select b.id from B b where b.id=a.id) then 'yes'
else 'no' end
/
when exists(select b.id from B b where b.id=a.id) then 'yes'
else 'no' end
yz394777014 回答的这个应该是OK的
update a.filed1 =
select b.filed2 form b where a.id = b.id这时a表中,所有记录都会更新。
所以要加上where ,再限定一次条件。update a.filed1 =
select b.filed2 form b where a.id = b.id
where ( exist slect 'X' from
a, b where a.id = b.id)以前碰到过类似的问题。
update a.filed1 =
select b.filed2 form b where a.id = b.id 这时a表中,所有记录都会更新。
所以要加上where ,再限定一次条件。 update a.filed1 =
select b.filed2 form b where a.id = b.id
where ( exist slect 'X' from
a, b where a.id = b.id) 以前碰到过类似的问题。
UPDATE A
SET A.STATUS = DECODE((SELECT 'true'
FROM B
WHERE EXISTS (SELECT '1'
FROM A C
WHERE C.ID = B.ID
AND C.ID = A.ID)),
'true',
'yes',
'no')
结果是出来了,就是不知道,上面还在讨论的是什么问题,很好奇。学习中