oracle表中删除重复项的问题
2015-08-28 00:00
656 查看
摘要: 在tab_shop_info表中,删除cust_id(非主键)重复的项,只保留主键(shop_id)最小的项。
(这段是原创的)
今天心血来潮,针对前几天韩哥说的一个会员只能创建一个商铺的问题整理一下tab_shop_info这张表,然后准备删掉会员id(cust_id)重复的项,而且只留下重复项里面id(shop_id)最小的项。
T_T 本来以为两三行写完的东西,真正写起来居然这么复杂------赶紧赶紧记下来!
第一步:查询重复项;
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
第二步:查询重复项里面shop_id最小的;
select min(shop_id) from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
第三步:统计所有重复项的shop_id;
select shop_id from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
第四步:排除掉shop_id最小的那个;
select shop_if from (
select shop_id from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)where shop_id not in(
select min(shop_id) from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)
第五步:删除
delete from tab_shop_info where shop_id in(
select shop_if from (
select shop_id from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)where shop_id not in(
select min(shop_id) from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)
)
OK,这样才算完工了!
但是总觉得这么写好复杂啊,如果有谁写过简单一些的写法,麻烦在下面留言告知一下哈。1+1>=2,谢谢啦!
(这段是原创的)
今天心血来潮,针对前几天韩哥说的一个会员只能创建一个商铺的问题整理一下tab_shop_info这张表,然后准备删掉会员id(cust_id)重复的项,而且只留下重复项里面id(shop_id)最小的项。
T_T 本来以为两三行写完的东西,真正写起来居然这么复杂------赶紧赶紧记下来!
第一步:查询重复项;
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
第二步:查询重复项里面shop_id最小的;
select min(shop_id) from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
第三步:统计所有重复项的shop_id;
select shop_id from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
第四步:排除掉shop_id最小的那个;
select shop_if from (
select shop_id from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)where shop_id not in(
select min(shop_id) from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)
第五步:删除
delete from tab_shop_info where shop_id in(
select shop_if from (
select shop_id from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)where shop_id not in(
select min(shop_id) from tab_shop_info where cust_id in (
select cust_id from tab_shop_info group by cust_id having count(cust_id) > 1
)
)
)
OK,这样才算完工了!
但是总觉得这么写好复杂啊,如果有谁写过简单一些的写法,麻烦在下面留言告知一下哈。1+1>=2,谢谢啦!
相关文章推荐
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- Oracle Containers for J2EE远程安全漏洞(CVE-2014-0413)
- 为什么 GROUP BY 之后不能直接引用原表中的列
- Oracle 10g R2不能使用EM的问题
- 表空间操作
- PreparedStatement中in子句的处理
- VMware下RedHat4.8_64位安装Oracle 10g RAC--简略脚本
- oracle sql日期比较
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- OS block size和Oracle block size,查找OS Blocksize的方法
- oracle中创建数据库和表空间的几点总结
- 数据库自动备份脚本
- oracle的nvl函数的使用介绍
- 解决oracle用户连接失败的解决方法
- oracle的一些tips技巧
- Oracle 下的开发日积月累
- Oracle存储过程之数据库中获取数据实例
- Windows下ORACLE 10g完全卸载的方法分析