当前位置:网站首页>mysql删除表中重复数据,(只保留一行)
mysql删除表中重复数据,(只保留一行)
2022-07-30 05:42:00 【就是叫这个名字】
示例表中数据如下:
根据uniqle_id这一列来判断数据是否重复
1.查看重复的数据有哪些
select uniqle_id,name from person group by uniqle_id having count(uniqle_id) > 1
结果如下:
2.删除重复的列,只保留id最小的一行
delete from person where uniqle_id in (
select * from
(select min(uniqle_id) from person group by uniqle_id having count(uniqle_id) > 1) temp
)
and id not in
(select * from
(select min(id) from person group by uniqle_id having count(uniqle_id)> 1 )temp2
)
3.删除所有重复的行
delete from person where uniqle_id in (
select * from
(select min(uniqle_id)as uniqle_id from person group by uniqle_id having count(uniqle_id) > 1) temp
)
(sql中的temp存在的作用是给子查询的结果起一个别称,如果不起这个别称的话,会报错:SQL 错误 [1248] [42000]: Every derived table must have its own alias)
边栏推荐
猜你喜欢
随机推荐
批量自动归集
C#中default关键字用法简介
史上超强最常用SQL语句大全
phpok website vulnerability exploitation analysis
【SQL】SQL 高频面试题英语版(1)
互联网商城盲盒app为何如此火爆
CTFSHOW command execution [web29-web124] unfinished to be continued
npm安装和npm安装——保存
DVWA安装教程(懂你的不懂·详细)
记一次流量分析实战——安恒科技(八月ctf)
关于浅拷贝和深拷贝,草稿闲了写
node包的导入与导出
php漏洞全解
npm基本使用
在线sql编辑查询工具sql-editor
Jackson 序列化失败问题-oracle数据返回类型找不到对应的Serializer
misc-log analysis of CTF
sqli-labs靶场 SQL注入学习 Less-1
【数仓】数据仓库高频面试题题英文版(1)
【OS】操作系统高频面试题英文版(1)

![[HCTF 2018]admin](/img/4e/58234ca163c22fc334334eb89a5b00.png)





