如何从Oracle SQL Developer中的两个表中删除重复数据

时间:2017-04-14 11:25:05

标签: sql oracle duplicates sql-delete

我的第一个表a包含列ID,invdate和invnumber以及VALUES:

(1, 01/10/13, 1)
(2, 02/10/13, 2)
(2, 02/10/13, 2)
(3, 03/10/13, 3)
(4, 04/10/13, 4)
(4, 05/10/13, 4)

我的第二个表b有列ID,DATEOFBIRTH,姓氏,带名字的名字:

(1, 19/06/1997, Fox, Migan)
(2, 30/07/1993, Todd, James)
(2, 30/07/1993, Todd, James)
(3, 1/08/1994, Xi, Ji)
(4, 2/07/1992, Fared, Bob)
(4, 2/07/1992, Fared, Bob) 

如何使用DELETE FROM()函数删除两个表中的所有重复行?我想我应该以某种方式合并表格然后删除重复的行但不太确定。

至于结果,

SELECT * FROM a 

应该给:

(1, 01/10/13, 1)
(2, 02/10/13, 2)
(3, 03/10/13, 3)
(4, 04/10/13, 4)

SELECT * FROM B; 

应该给:

(1, 19/06/1997, Fox, Migan)
(2, 30/07/1993, Todd, James)
(3, 1/08/1994, Xi, Ji)
(4, 2/07/1992, Fared, Bob)

更新(解决方案):

DELETE FROM WHERE rowid>(SELECT min(rowid)FROM b WHERE a.ID = b.ID);

有人可以帮我解释一下这个sql吗?

1 个答案:

答案 0 :(得分:1)

以下是您的" b"表

取自http://www.dba-oracle.com/t_delete_duplicate_table_rows.htm

(你也可以为你的" a"表做同样的事情)

  DELETE FROM 
  b  t1
  WHERE 
  t1.rowid > 
  ANY ( SELECT  t2.rowid FROM  b t2
       WHERE 
          t1.id = t2.id AND  t1.DATEOFBIRTH = t2.DATEOFBIRTH AND t1.lastname = t2.lastname AND  t1.firstname  = t2.firstname 
       );