mysql处理重复数据仅保留一条记录
目的:去除(或删除)一个表里面手机号重复的数据,但是需要保留其中一个记录,换句话说,表里面手机号不为空的数据,一个手机有且只有一条记录
表结构:
CREATE TABLE `account` (
`id` int(11) NOT NULL,
`phone` varchar(11) DEFAULT NULL,
PRIMARY KEY (`id`)
);
插入一些数据:
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
insert into account values('','');
查询一下现在表里面的重复情况:
select count(*)as num,phone from account where phone <> '' group by phone having num > 1;
查询结果:

现在我们要去除多余的手机号数据,直接把这个值置为空,删除同理,这里不再重复
SQL如下:
update account set phone='' where
phone in
(select phone from account where phone<>'' group by phone having count(id)>1)
and
id not in
(select min(id) from account where phone<>'' group by phone having count(id)>1)
;
但是执行的时候会报错:
update account set phone='' where phone in (select phone from account where phone<>'' group by phone having count(id)>1)and id not in(select min(id) from account where phone<>'' group by phone having count(id)>1)
Error Code: 1093. You can't specify target table 'account' for update in FROM clause 0.031 sec
分析:不能先select出同一表中的某些值,再update这个表(在同一语句中)
重新写SQL(取下别名):
update account set phone='' where
phone in
(select a.phone from
(select phone from account where phone<>'' group by phone having count(id)> 1) as a
)
and
id not in
(select b.id from
(select min(id) as 'id' from account where phone<>'' group by phone having count(id)> 1) as b
)
;
执行完成之后我们再查一遍数据的情况:
select * from account;
查询结果(每个手机号只有一条记录,其他均被置空):

mysql处理重复数据仅保留一条记录的更多相关文章
- mysql 删除重复数据只保留一条记录
删除重复数据保留name中id最小的记录 delete from order_info where id not in (select id from (select min(id) as id fr ...
- mysql删除重复数据只保留一条
mysql删除重复数据只保留一条 新建一张测试表: CREATE TABLE `book` ( `id` char(32) NOT NULL DEFAULT '', `name` varchar(10 ...
- MySql删除重复数据并保留一条
DELETE FROM tbl_1 WHERE id NOT IN( SELECT id FROM ( SELECT min(id) AS id FROM tbl_1 GROUP BY `duplic ...
- 面试题中经常遇到的SQL题:删除重复数据,保留其中一条
如题,解决思路如下: 1.首先我们需要找出拥有重复数据的记录 ---以name字段分组 select Name,COUNT(Name) as [count] from Permission group ...
- oracle删除重复数据只保留一条
-- 如表role_user的数据 ROLEID USERID -- 删除相同记录只剩下一条记录 根据两个字段查询重复数据 (roleid,userid) ) 删除重复数据只保留一条 delete f ...
- mysql删除重复数据,保留最新的那一条
因为数据库没键外键,在关联查询的时候,会碰到查询条数多余数据库实际条数,这因为关联字段在表中有重复值而导致的. 解决方案: 1.数据库脚本删除重复数据,保留最新的一条 2.对关联字段增加唯一约束 例如 ...
- mysql语句删除重复数据,保留一条;查询所有重复数据;查询重复数据的一条,
//显示重复的所有条 SELECT * FROM 表名 WHERE (字段1,字段2,...) IN (SELECT 字段1,字段2,...FROM 表名 GROUP BY 字段1,字段2,... H ...
- MySQL中删除重复数据只保留一条
用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT ...
- SQL Server 删除重复数据只保留一条
DELETE FROM Bus_TerminalMessage_Keywords WHERE Content IN (select Content from Bus_TerminalMessage_K ...
随机推荐
- pg_restore - 从一个由 pg_dump 创建的备份文件中恢复 PostgreSQL 数据库。
SYNOPSIS pg_restore [ option...] [ filename] DESCRIPTION 描述 pg_restore 是一种用于恢复由 pg_dump(1) 创建的任何非纯文本 ...
- MYSQL学习笔记——sql语句优化之索引
上一篇博客讲了可以使用慢查询日志定位耗时sql,使用explain命令查看mysql的执行计划,以及使用profiling工具查看语句执行真正耗时的地方,当定位了耗时之后怎样优化呢?这篇博客会介绍my ...
- 02cython调用c++文件
https://blog.csdn.net/ztf312/article/details/77340300 此时用python setup.py build_ext --inplace编译时报错如下: ...
- Oracle Set操作
并集合 union/uinon all union 会去重,uinon all 不去重 交集 intersect 差集 minus
- AOP拦截日志类,抛异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode
AOP的日志拦截类中,抛出异常: java.lang.IllegalStateException: It is illegal to call this method if the current r ...
- maven 常用插件 拷贝依赖 拷贝jar包 查看属性 环境变量
1 maven编译后希望将生产的jar包拷贝到指定目录 在pom中配置maven插件 maven-antrun-plugin <build > <plugins> <pl ...
- Python---基础---常用的内置模块(Github、P有charm、math数学模块和random随机数模块,做一些简单的练习)
2019-05-24 ----------------------------------
- 027:for标签使用详解
for标签使用详解: for...in... 标签: for...in... 类似于 Python 中的 for...in... .可以遍历列表.元组.字符串.字典等一切可以遍历的对象.示例代码如下: ...
- HashMap底层代码分析
public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; //this.loadFactor为加载因子,其值为默认的加载因子常量:DEFAUL ...
- swift中为什么要创造出可选型?
(1)因为nil这个东西,swift中没有就是没有. Int? 叫 整型可选型,如果不提前声明,直接赋值变量 nil会报错 . 可以将Int赋值给Int? ,但是不能将Int?赋值给Int . ...