【问题标题】:Delete duplicate rows in a table in sql在sql中删除表中的重复行
【发布时间】:2017-11-22 20:33:31
【问题描述】:

在 oracle 11g 中对此结果的查询是什么

我有一个表调用者 product_table.in 我的 product_table 我没有主键,我有这些记录

pid  pname   pprice
===  =====   ======
101   aaa      343
101   aaa      343
101   aaa      343
101   aaa      343

我想要一个查询,它将删除除一条记录之外的所有重复值

表示执行该查询后我需要此结果

pid  pname   pprice
===  =====   ======
101   aaa      343

可以不使用任何函数或过程逻辑的sql查询或子查询吗??

提前致谢

【问题讨论】:

  • 这个表上是否有某种类型的索引?
  • 也许可以试试this

标签: sql oracle


【解决方案1】:

在 Oracle 中,您可以使用 rowid 和子查询来执行此操作:

delete from product_table
    where rowid not in (select min(rowid)
                        from product_table
                        group by pid, pname, pprice
                       );

【讨论】:

    【解决方案2】:

    Gordon Linoff 先生的回答是正确的,但这也可以使用 self join

    来实现
    delete from product_table p1
      where rowid not in
      (select max(rowid) from product_table p2
      where p1.pid = p2.pid );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 2019-10-10
      • 2013-02-13
      • 1970-01-01
      相关资源
      最近更新 更多