您的位置:首页 > 数据库

【SQL】通过rowid查找及删除重复记录

2017-04-30 21:40 281 查看
新建T表如下:

SQL> select * from t;

         X Y

---------- --

         1 a

         1 a

         1 a

         2 b

         2 b

         3 a

         3 a

1.查询表中重复的记录(在子查询中运用了自连接查出相同记录的max(rowid),通过不等值运算查出去,除了第一条重复记录后的重复记录)

SQL> select x,y from t

  2  where rowid!=(select max(rowid) from t a

  3  where t.x=a.x

  4  and

  5  t.y=a.y);

 

         X Y

---------- --

         1 a

         1 a

         2 b

         3 a

2.删除重复记录

SQL> delete from t

  2  where rowid!=(select max(rowid) from t a

  3  where t.x=a.x

  4  and

  5  t.y=a.y);

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: