博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL中删除重复数据只保留一条
阅读量:5352 次
发布时间:2019-06-15

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

用SQL语句,删除掉重复项只保留一条

在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

SELECT    *FROM    peopleWHERE    peopleId IN (        SELECT            peopleId        FROM            people        GROUP BY            peopleId        HAVING            count(peopleId) > 1    )

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

DELETEFROM    peopleWHERE    peopleName IN (        SELECT            peopleName        FROM            people        GROUP BY            peopleName        HAVING            count(peopleName) > 1    )AND peopleId NOT IN (    SELECT        min(peopleId)    FROM        people    GROUP BY        peopleName    HAVING        count(peopleName) > 1)

3、查找表中多余的重复记录(多个字段)

SELECT    *FROM    vitae aWHERE    (a.peopleId, a.seq) IN (        SELECT            peopleId,            seq        FROM            vitae        GROUP BY            peopleId,            seq        HAVING            count(*) > 1    )

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

DELETEFROM    vitae aWHERE    (a.peopleId, a.seq) IN (        SELECT            peopleId,            seq        FROM            vitae        GROUP BY            peopleId,            seq        HAVING            count(*) > 1    )AND rowid NOT IN (    SELECT        min(rowid)    FROM        vitae    GROUP BY        peopleId,        seq    HAVING        count(*) > 1)

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

SELECT    *FROM    vitae aWHERE    (a.peopleId, a.seq) IN (        SELECT            peopleId,            seq        FROM            vitae        GROUP BY            peopleId,            seq        HAVING            count(*) > 1    )AND rowid NOT IN (    SELECT        min(rowid)    FROM        vitae    GROUP BY        peopleId,        seq    HAVING        count(*) > 1)

6.消除一个字段的左边的第一位:

UPDATE tableNameSET [ Title ]= RIGHT ([ Title ],(len([ Title ]) - 1))WHERE    Title LIKE '村%'

7.消除一个字段的右边的第一位:

UPDATE tableNameSET [ Title ]= LEFT ([ Title ],(len([ Title ]) - 1))WHERE    Title LIKE '%村'

8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录

UPDATE vitaeSET ispass =- 1WHERE    peopleId IN (        SELECT            peopleId        FROM            vitae        GROUP BY            peopleId

 

转载于:https://www.cnblogs.com/jiangxiaobo/p/6589541.html

你可能感兴趣的文章
【BZOJ-1055】玩具取名 区间DP
查看>>
Bit Twiddling Hacks
查看>>
LeetCode : Reverse Vowels of a String
查看>>
时间戳与日期的相互转换
查看>>
jmeter(五)创建web测试计划
查看>>
python基本数据类型
查看>>
1305: [CQOI2009]dance跳舞 - BZOJ
查看>>
关于TDD的思考
查看>>
Cocos2d-x学习之windows 7 android环境搭建
查看>>
将html代码中的大写标签转换成小写标签
查看>>
jmeter多线程组间的参数传递
查看>>
零散笔记
查看>>
MaiN
查看>>
[Python学习] 简单网络爬虫抓取博客文章及思想介绍
查看>>
触发器课程SQL Server 知识梳理九 触发器的使用
查看>>
信息浏览器从Android的浏览器中传递cookie数据到App中信息浏览器
查看>>
客户端连接linux虚拟机集群报错
查看>>
linux下部署一个JavaEE项目的简单步骤
查看>>
hash储存机制
查看>>
[Android学习系列16]Android把php输出的json加载到listview
查看>>