【问题标题】:Deleting from Oracle SQL table using 'inner join'使用“内部联接”从 Oracle SQL 表中删除
【发布时间】:2016-10-21 20:56:06
【问题描述】:

所以我搜索了高低,尝试了此论坛上使用的其他提示均无济于事。

因此尝试在 Oracle SQL Developer (v3.2.20.09) 中使用内部联接删除

我希望从(Table1,列名 Column1)中删除的表,其中数据与“Table2”中的“Column2”列匹配。

我知道 Oracle/Microsoft SQL 之间存在一些差异,尝试了多个查询,如下所示,略有不同(使用开/关括号、内连接、WHERE EXISTS、WHERE(选择)。 试一试:

 delete from table2 where 
 exists (select column1 from table1);

 delete from table2, 
 inner join table1 on table2.column2 = table1.column1;

我写的代码有什么问题?

【问题讨论】:

  • 考虑使用MERGE。它包含一个您可以使用的DELETE 子句。

标签: sql oracle join


【解决方案1】:

EXISTS 版本如下所示:

delete from table2
where exists (select *
              from table1
              where table1.column1 = table2.column2);

您也可以使用IN 子句

delete from table2
where column2 in (select column1
                  from table1);

【讨论】:

    【解决方案2】:

    如果您尝试从table1 中删除,那么这是必须在delete 子句中使用的表名,而不是table2

    delete table1 t1
     where exists (select null
                     from table2 t2
                    where t2.column2 = t1.column1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 2017-12-22
      • 2013-11-08
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多