博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
删除数据库重复的记录
阅读量:7222 次
发布时间:2019-06-29

本文共 1384 字,大约阅读时间需要 4 分钟。

hot3.png

如果数据库中有重复数据,则对重复的数据保留一条,其他删出,以交易日期表举例,关联字段为唯一索引字段。

one:

delete exchangedate a where exists(select 1                from (select *                        from (select init_date,                                     finance_type,                                     exchange_type,                                     min(rowid) as row_id,                                     count(*) as row_count                                from exchangedate                            group by init_date, finance_type, exchange_type)                       where row_count > 1) b               where a.finance_type = b.finance_type                 and a.exchange_type = b.exchange_type                 and a.init_date = b.init_date                 and a.rowid <> b.row_id);

two:

delete exchangedate a where a.rowid > (select min(rowid)                     from exchangedate b                    where a.finance_type = b.finance_type                     and a.exchange_type = b.exchange_type                     and a.init_date = b.init_date);

扩展:按某个字段分组,然后将这个字段下数据只保留一条记录。

delete hs_user.functiontomenu awhere exists(select 1                from (select menu_id,min(rowid) as row_id                       from hs_user.functiontomenu                   group by menu_id) b              where a.menu_id = b.menu_id                and a.rowid > b.row_id);

 

转载于:https://my.oschina.net/u/2332532/blog/691245

你可能感兴趣的文章
RxJava -- fromArray 和 Just 以及 interval
查看>>
LC #75 JS
查看>>
js正则验证代码库
查看>>
常见面试题—css实现垂直水平居中
查看>>
lc682. Baseball Game
查看>>
重学前端-css选择器
查看>>
iOS开发之扫描二维码
查看>>
Android黑科技: 快速找到view所在的xml文件
查看>>
linux分区方案
查看>>
003-Java技术体系
查看>>
超轻量模板引擎
查看>>
JavaScript 复习之 Object对象的相关方法
查看>>
JAVA之流程控制语句
查看>>
Spring Boot(1)
查看>>
Winodws 10 美化与调优
查看>>
apache安装及多域名解析及域名代理
查看>>
什么是自动化运维 ? 自动化运维的设计思路以及实战
查看>>
Python练习实例100例(持续更新中)
查看>>
非父组件通信
查看>>
Electron系列文章-主进程与渲染进程
查看>>