oracle 如何使用join和where子句从一个表中删除另一个表中的值

z3yyvxxp  于 2023-08-04  发布在  Oracle
关注(0)|答案(1)|浏览(90)

我需要从一个表中删除数据,但对where子句使用另一个表。所以我只想删除输出中的值,而不是其他表中的任何值。
我要从中删除数据的表有2行:ot_id和classification_id

select 
t0.*
from ot_classification t0
left join
ot t1
on t0.ot_id=t1.id
where t1.ct = '22GC'
and t0.classification_id not in '157101'

字符串
我认为我应该做的是:

delete t0.ot,
t0.classification_id
from ot_classification t0
left join
ot t1
on t0.ot_id=t1.id
where t1.ct = '22GC'
and t0.classification_id not in '157101'


我担心这也会删除t1.id,这不是我想要的。我该怎么避免呢?

qxsslcnc

qxsslcnc1#

Oracle不支持您的delete语法。
我推荐exists

delete from ot_classification t0
where 
    t0.classification_id != 157101
    and exists (
        select 1
        from ot t1
        where  t0.ot_id = t1.id and t1.ct = '22GC'
)

字符串
在Oracle 23c中,我们也可以这样描述查询:

delete ot_classification t0
from ot t1 
where 
    T0.ot_id = t1.id 
    and t1.ct = '22GC' 
    and t0.classification_id != 157101

相关问题