我在Oracle 11中有两个表:
表格ta_one:
name | active
----------------
john | yes
tina | yes
mike | yes
表ta_two:
name | active
----------------
john | yes
tina | yes
tina | no
mike | no
我想要实现的目标如下: 我想更新专栏" active"在第一个表ta_one中,列的值为#34;有效"在以下条件下从第二个表ta_two:
因此,更新后表ta_one应如下所示:
name | active
----------------
john | yes
tina | yes
mike | no
只有迈克的参赛作品发生了变化,因为蒂娜在第二张桌子上的次数不止一次,约翰的参赛作品中有一个是“是”。在表ta_two的活动列中。
提前感谢您的帮助。
答案 0 :(得分:0)
我总是使用值'yes'
进行更新,因为您的逻辑只会执行此操作。
UPDATE ta_one
SET active = 'no'
WHERE EXISTS
(
SELECT t2.active
FROM ta_one t1
INNER JOIN ta_two t2
ON t1.name = t2.name
INNER JOIN
(
SELECT name
FROM ta_two
GROUP BY name
HAVING COUNT(*) = 1
) t3
ON t1.name = t3.name
WHERE t2.active = 'no' AND
ta_one.name = t1.name
);
答案 1 :(得分:0)
可能符合以下条件:
update
ta_one o
set
o.active='no'
where o.name in (select name from ta_two where active='no')
and (select count(*) from ta_two where name=o.name)=1
答案 2 :(得分:0)
update ta_one
set active =
(select active from ta_two where ta_one.name = ta_two.name)
where 1 = (select count(1)
from ta_two
where ta_two.name = ta_one.name
group by name)
答案 3 :(得分:0)
更新ta_one x
set active =(选择y.active
来自ta_two y
其中y.name = x.name
)
哪里不存在(
选择1
来自ta_two y
其中y.name = x.name
按名称分组
有计数(明显y.active)> 1
)