MySQL和Oracle关于重复数据的操作(查找、删除)
来源:互联网 发布:淘宝上买笔记本可靠吗 编辑:程序博客网 时间:2024/05/29 08:39
假设有employee这张表,其中emp_name可能有重复,emp_id是主键(没有主键的写法在最后)
以下是MySQL和Oracle中关于重复数据的删除查找操作
查询重复数据:
Oracle、MySQL
select * from employee group by emp_name having count (*)>1;
查询可以删除的重复数据:
Oracle:
select t1.* from employee t1 where t1.emp_name in (SELECT t2.emp_name from employee t2 group by emp_name having count (*)>1) and t1.emp_id not in(select min(t3.emp_id) from employee t3 group by emp_name having count (*)>1);MySQL:
select t1.* from employee t1 where t1.emp_name in (select t4.emp_name from (select t2.emp_name from employee t2 group by t2.emp_name having count(*)>1) t4) andt1.emp_id not in (select t5.emp_id from (select min(t3.emp_id) as emp_id from employee t3 group by t3.emp_name having count(*)>1) t5);
删除重复数据:
Oracle:
delete t1 from employee t1 where t1.emp_name in (SELECT t2.emp_name from employee t2 group by emp_name having count (*)>1) andt1.emp_id not in (select min(t3.emp_id) from employee t3 group by emp_name having count (*)>1);
MySQL:
delete t1 from employee t1 where t1.emp_name in (select t4.emp_name from (select t2.emp_name from employee t2 group by t2.emp_name having count(*)>1) t4) and t1.emp_id not in (select t5.emp_id from (select min(t3.emp_id) as emp_id from employee t3 group by t3.emp_name having count(*)>1) t5);
没有主键的情况下删除重复记录是面试时经常会问到的,此时Oracle和MySQL的处理方法也有所不同。
Oracle:
主要利用了rowid,rowid它是Oracle的一个伪列,它并不实际存在于表中。它是ORACLE在读取表中数据行时,根据每一行数据的物理地址信息编码而成的一个伪列。我们可以把它当成一条记录的唯一标识来使用。只保留一个rowid最大或者最小的记录即可。如果主键是number类型,也可以使用这种方法,把rowid换成主键名即可。
delete from employee t1 wheret1.rowid > (select min(t2.rowid) from employee t2 where t1.emp_name=t2.emp_name);
MySQL:
MySQL没有类似Oracle的rowid,所以操作会比较麻烦。
1.查询重复记录,将查询的数据插入一个新的表中
2.删除原来的表的重复数据
3.将新表的数据再插入原表中
4.删除新表
create table new_table(select * from employee group by emp_name having count(*)>1);
delete employee.* from employee where emp_name in(select t.* from(select emp_name from employee group by emp_name having count(*)>1) t);第二步这里查出的重复数据需要嵌套一层查询。如果先查出同一表中的某些值,再修改这个表的内容(在同一语句中)会出现 You can't specify target table 的错误。
insert into employee (select * from new_table);
drop table new_table;
阅读全文
0 0
- MySQL和Oracle关于重复数据的操作(查找、删除)
- Oracle 查找、删除重复数据
- 关于oracle查找重复数据
- mysql、oracle删除重复数据
- oracle 重复数据的排查和删除
- sqlserver sql 对重复数据的操作 查找 删除
- Mysql中查找并删除重复数据的方法
- mysql中查找并删除重复数据
- MySQL中,查找并删除重复数据
- Oracle 查找重复数据,删除重复数据只保留一项
- Oracle中查找和删除重复记录的方法总结
- MySQL 中查找重复数据,删除重复数据
- Mysql中查找重复数据,删除重复数据
- mysql 删除重复的数据
- 关于MySql删除表中重复数据
- 重复数据的查找,更新,删除
- MySQL之生成唯一的序列号和删除重复数据
- mysql 查找重复数据
- Project facet Java version 1.7 is not supported.
- cocoapods常见错误
- 2013 NOIP复赛 提高组 C++
- 嵌入式处理器用能量采集技术特别报告
- @Sharable 注解作用
- MySQL和Oracle关于重复数据的操作(查找、删除)
- unittest使用小demo
- MYSQL查询语句优化方法
- http请求报包含三个部分
- springboot学习笔记
- SpringMVC中出现" 400 Bad Request "错误的解决方法
- [Codeforces] 847B
- 冒泡排序
- view.py 代码