有这样一张表,表数据及结果如下:
school_id school_name total_student test_takers 1239 Abraham Lincoln High School 55 50 1240 Abraham Lincoln High School 70 35 1241 Acalanes High School 120 89 1242 Academy Of The Canyons 30 30 1243 Agoura High School 89 40 1244 Agoura High School 100 50
我们可以看出,school_name的字段值有重复数据(Abraham Lincoln High School 和Agoura High School分别出现两次),那么如何删除这两条数据,从而只让这两个数值出现一次呢? 具体实现方法如下:
1、删除重复记录,保存Id最小的一条
delete FROM `test` WHERE `school_name` in (SELECT `school_name` FROM `test` GROUP BY `school_name` HAVING COUNT( * ) >1) and school_id not in (select min(school_id) from test group by school_id having count(* )>1)
先使用GROUP BY having语法查询出重复的数据,然后删除重复数据并保留school_id最小的一条.
2、删除重复记录,保存Id最大的一条
delete FROM `test` WHERE `school_name` in (SELECT `school_name` FROM `test` GROUP BY `school_name` HAVING COUNT( * ) >1) and school_id not in (select max(school_id) from test group by school_id having count(* )>1)
原理和上面一样。
以上就是mysql查找删除重复数据并只保留一条实例详解,希望能帮助到大家,谢谢大家对本站的支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
更新日志
- 柏菲·胭花四乐《胭花四乐》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 群星《监听耳机天碟》2018[WAV分轨][1G]
- 群星《娱协奖原创金曲合辑》滚石[WAV+CUE][1.1G]
- 罗大佑《美丽岛》2CD[WAV+CUE][1.1G]
- 言承旭.2009-多出来的自由【SONY】【WAV+CUE】
- 赤道.2000-精选2CD【ACM】【WAV+UCE】
- 许廷铿.2017-神奇之旅【华纳】【WAV+CUE】
- 李克勤《罪人》环球[WAV+CUE][1G]
- 陈粒2024《乌有乡地图》有此山文化[FLAC分轨][1G]
- 蔡依林《MYSELF》 奢华庆菌版 2CD[WAV+CUE][1.5G]
- 刘春美《心与心寻世界名曲中文版》新京文[低速原抓WAV+CUE]
- 朱逢博《蔷薇蔷薇处处开》[FLAC+CUE]
- 姚璎格2005《心在哭泣》龙韵[WAV分轨]
- 费玉清《费玉清收藏》 2CD 华纳[WAV+CUE][1G]
- 徐怀钰《LOVE》台湾首版[WAV+CUE][1G]