sql经典的删除表中重复数据

sql语句:

                delete  from 表名 t1 where rowid != (select min(rowid) from 表名 t2 where t1.字段= t2.字段);

          or:

                 delete  from 表名 t1 where rowid != (select max(rowid) from 表名 t2 where t1.字段= t2.字段);

说明:

         在oracle中,rowid是虚列,rowid是物理地址,用于定位oracle中具体数据的物理存储位置。通俗的讲:rowid是相对不变的。

疑问:

对于小括号中的sql语句(select min(rowid) from 表名 t2 where t1.字段= t2.字段),尽管能够读懂,明白根据where条件查询出来的min(rowid)有多个结果。但是对于它的执行过程时存在疑问的!

疑问解答:

                 where后面的条件是每走一行数据就执行一次的,所以能存在多个min(rowid)或者多个max(rowid),where t1.字段=t2.字段是一个动态的赋值的过程。

第二种方式:

delete from 表名 t1 where rowid not in (select min(rowid) from 表名 t group by t.uname);

用这种也可以删除成功!也更好理解一些,不过效率比较低!

猜你喜欢

转载自gaoquanyang.iteye.com/blog/1547122
今日推荐