【问题标题】:Oracle - deleting duplicatesOracle - 删除重复项
【发布时间】:2016-02-21 11:26:10
【问题描述】:

我找到了以下删除重复项的方法:

DELETE FROM
   table_name A
WHERE
  a.rowid >
   ANY (
     SELECT
        B.rowid
     FROM
        table_name B
     WHERE
        A.col1 = B.col1
     AND
        A.col2 = B.col2
        ); 

有人可以逐步解释一下查询是如何工作的吗?

TIA!

【问题讨论】:

    标签: oracle duplicates


    【解决方案1】:

    在Oracle 中,ROWID 是一个伪列,指向一行的物理位置。查询执行自连接并获取具有相同值的第 1 列和第 2 列的那些行 - 假设这些键足以识别为重复行。

    一旦获取了行,查询就会删除那些大于获取的第一行的rowid,从而删除重复项

    【讨论】:

    • 非常感谢!我现在好多了!
    【解决方案2】:

    像这样:

    DELETE FROM         // The command to delete
       table_name A     //the table in which you want to remove duplicate
    WHERE               //condition
      a.rowid >         //checking the rowid which oracle adds to each row. Oracle Database rowid values contain information necessary to locate a row.
       ANY (             //any row which has a condition
         SELECT          //select
            B.rowid      //rowid
         FROM             //from  
            table_name B    //table name with alias name as B. Is used for making a self join
         WHERE               //condition
            A.col1 = B.col1    //where the column1 has the same rowid 
         AND                    //and
            A.col2 = B.col2     //where the column2 has the same rowid 
            ); 
    

    【讨论】:

    • 非常感谢。不幸的是,我不能同时接受这两个答案。但我现在理解得更好了。顺便说一句:我认为//where the column1 has the same rowid 应该是//where the column1 has the same condition。
    • @MaMu:- 不客气!如果您接受其他答案,则没有问题。应该接受对您有帮助的答案。 :)
    猜你喜欢
    • 2020-09-13
    • 2020-10-23
    • 2020-02-17
    • 1970-01-01
    • 2021-11-08
    • 2010-10-25
    • 1970-01-01
    • 2018-04-07
    • 2011-03-24
    相关资源
    最近更新 更多