子查询删除不在oracle中工作

时间:2016-08-03 12:16:41

标签: sql oracle sql-delete

我有以下查询加入并加入:

   select aad.id
                  from [table1] aad
                  left outer join [table2] itm
                    on aad.table2_id = itm.id
                  left outer join [table3] eac
                    on aad.id = eac.table1_id
                  LEFT JOIN [table4] ces
                    ON eac.car_id = ces.id
                  LEFT join [table5] ci
                    on ces.car_information_id = ci.id
                 INNER join [table6] groupBi
                    on aad.capatibilty_degree_group_id = groupBi.id
                 where ces.id is null
                   and aad.depot_ammu_estimate = 123

上述查询的结果是:

id
-----
2433
2431

[table1]表(aad.id)的ID然后我想删除该表的这些记录然后我查询以下语法:

delete
  FROM [table1] w
 where w.id in (select aad.id
                  from [table1] aad
                  left outer join [table2] itm
                    on aad.table2_id = itm.id
                  left outer join [table3] eac
                    on aad.id = eac.table1_id
                  LEFT JOIN [table4] ces
                    ON eac.car_id = ces.id
                  LEFT join [table5] ci
                    on ces.car_information_id = ci.id
                 INNER join [table6] groupBi
                    on aad.capatibilty_degree_group_id = groupBi.id
                 where ces.id is null
                   and aad.depot_ammu_estimate = 123)

怎么回事,没有要删除的记录。我不知道上面的查询没有删除记录的情况。

3 个答案:

答案 0 :(得分:1)

我认为你的问题是在查询中使用“is null”。我不知道为什么会发生这个问题

delete
  FROM [table1] w
 where w.id in (select aad.id
                  from [table1] aad
                  left outer join [table2] itm
                    on aad.table2_id = itm.id
                  left outer join [table3] eac
                    on aad.id = eac.table1_id
                  LEFT JOIN [table4] ces
                    ON eac.car_id = ces.id
                  LEFT join [table5] ci
                    on ces.car_information_id = ci.id
                 INNER join [table6] groupBi
                    on aad.capatibilty_degree_group_id = groupBi.id
                 where nvl(ces.id,0)=0 
                   and aad.depot_ammu_estimate = 123)

答案 1 :(得分:1)

ces.id is null替换为nvl(ces.id,0) = 0

答案 2 :(得分:0)

我没有测试env来使用语法,但尝试使用EXIST子句。这样的事情。

DELETE FROM [table1] t WHERE
EXISTS
(
  select 1
                  from [table1] aad
                  left outer join [table2] itm
                    on aad.table2_id = itm.id
                  left outer join [table3] eac
                    on aad.id = eac.table1_id
                  LEFT JOIN [table4] ces
                    ON eac.car_id = ces.id
                  LEFT join [table5] ci
                    on ces.car_information_id = ci.id
                 INNER join [table6] groupBi
                    on aad.capatibilty_degree_group_id = groupBi.id
                 where ces.id is null
                   and aad.depot_ammu_estimate = 123 
and aad.id=t.id;
)