本文章总结了关于在mysql数据库中各种删除重复记录的sql语句,下面我们用实例介绍了操作方法,大家可参考一下。
MYSQL数据库中,经常会遇到重复记录的情况,那么就需要SQL删除重复记录,下面为您列举了四种删除重复记录的方式,用于不同的情况,希望对您有所帮助。
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
代码如下 | 复制代码 |
select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) |
2、SQL删除重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
代码如下 | 复制代码 |
delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1) and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1) |
3、查找表中多余的重复记录(多个字段)
代码如下 | 复制代码 |
select * from vitae a where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1) |
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
代码如下 | 复制代码 |
delete from vitae a where (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) |
演示数据
表结构:
代码如下 | 复制代码 |
mysql> desc demo; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(11) unsigned | NO | PRI | NULL | auto_increment | | site | varchar(100) | NO | MUL | | | +-------+------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
+----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.111cn.netn | | 2 | http://安卓主题_www.hzhuti.com | | 3 | http://www.zhutiy.com | | 4 | http://www.111cn.netn | | 5 | http://www.zhutiy.com | +----+------------------------+ 5 rows in set (0.00 sec) |
当没有创建表或创建索引权限的时候,可以用下面的方法:
如果你要删除较旧的重复记录,可以使用下面的语句:
代码如下 | 复制代码 |
mysql> delete from a -> using demo as a, demo as b -> where (a.id > b.id) -> and (a.site = b.site); Query OK, 2 rows affected (0.12 sec)
mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.111cn.netn | | 2 | http://安卓主题_www.hzhuti.com | | 3 | http://www.zhutiy.com | +----+------------------------+ 3 rows in set (0.00 sec) |
如果你要删除较新的重复记录,可以使用下面的语句:
代码如下 | 复制代码 |
mysql> delete from a -> using demo as a, demo as b -> where (a.id -> and (a.site = b.site); Query OK, 2 rows affected (0.12 sec)
mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 2 | http://安卓主题_www.hzhuti.com | | 4 | http://www.111cn.netn | | 5 | http://www.zhutiy.com | +----+------------------------+ 3 rows in set (0.00 sec) |
你可以用下面的语句先确认将被删除的重复记录:
代码如下 | 复制代码 |
mysql> SELECT a.* -> FROM demo a, demo b -> WHERE a.id > b.id -> AND (a.site = b.site); +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.111cn.netn | | 3 | http://www.zhutiy.com | +----+------------------------+ 2 rows in set (0.00 sec) |
如果有创建索引的权限,可以用下面的方法:
在表上创建唯一键索引:
代码如下 | 复制代码 |
mysql> alter ignore table demo add unique index ukey (site); Query OK, 5 rows affected (0.46 sec) Records: 5 Duplicates: 2 Warnings: 0
mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.111cn.netn | | 2 | http://安卓主题_www.hzhuti.com | | 3 | http://www.zhutiy.com | +----+------------------------+ 3 rows in set (0.00 sec)
|
重复记录被删除后,如果需要,可以删除索引:
代码如下 | 复制代码 |
mysql> alter table demo drop index ukey; Query OK, 3 rows affected (0.37 sec) Records: 3 Duplicates: 0 Warnings: 0 |
如果有创建表的权限,可以用下面的方法:
创建一个新表,然后将原表中不重复的数据插入新表:
代码如下 | 复制代码 |
mysql> create table demo_new as select * from demo group by site; Query OK, 3 rows affected (0.19 sec) Records: 3 Duplicates: 0 Warnings: 0
mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | demo | | demo_new | +----------------+ 2 rows in set (0.00 sec)
mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.111cn.netn | | 2 | http://安卓主题_www.hzhuti.com | | 3 | http://www.zhutiy.com | | 4 | http://www.111cn.netn | | 5 | http://www.zhutiy.com | +----+------------------------+ 5 rows in set (0.00 sec)
mysql> select * from demo_new order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.111cn.netn | | 2 | http://安卓主题_www.hzhuti.com | | 3 | http://www.zhutiy.com | +----+------------------------+ 3 rows in set (0.00 sec) |
然后将原表备份,将新表重命名为当前表:
代码如下 | 复制代码 |
mysql> rename table demo to demo_old, demo_new to demo; Query OK, 0 rows affected (0.04 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | demo | | demo_old | +----------------+ 2 rows in set (0.00 sec)
mysql> select * from demo order by id; +----+------------------------+ | id | site | +----+------------------------+ | 1 | http://www.111cn.netn | | 2 | http://安卓主题_www.hzhuti.com | | 3 | http://www.zhutiy.com | +----+------------------------+ 3 rows in set (0.00 sec) |
注意:使用这种方式创建的表会丢失原表的索引信息!
代码如下 | 复制代码 |
mysql> desc demo; +-------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+-------+ | id | int(11) unsigned | NO | | 0 | | | site | varchar(100) | NO | | | | +-------+------------------+------+-----+---------+-------+ 2 rows in set (0.00 sec) |
如果要保持和原表信息一致,你可以使用 show create table demo; 来查看原表的创建语句,然后使用原表的创建语句创建新表,接着使用 insert … select 语句插入数据,再重命名表即可。
实例
今天无意导入几个测试数据,发现测试数据中,有很多数据记录是相同的,我现在就想删除掉这些数据,
如何查询数据相同记录呢?这个好说,以下语句就可以查看相同记录的了:
代码如下 | 复制代码 |
SELECT COUNT(*) AS c, key_word FROM search_keywrod GROUP BY key_word HAVING c > 1 |
其中 HAVING c >1 代表相同记录数就有相同的了。
查询相同的是比较容易实现的了,但是想要删除这些重复的,估计就比较麻烦的了,因为你是要删除自身表里的记录,有些朋友可能就会使用到临时表,把相同需要删除的记录,导到时这个临时表,然后再通过临时表来删除主表。或者写一个临时程序,删除掉其中的一条重复记录。
以上二个方法删除重复记录,最大的麻烦就是操作繁琐。而我们今天介绍的就是利用MYSQL自身的语句,不创建临时表,不写程序来删除掉自身的重复记录。请看以下SQL语句:
代码如下 | 复制代码 |
SELECT t1.id, t1.key_word FROM search_keywrod t1, ( SELECT key_word, MIN(id) AS minid FROM search_keywrod GROUP BY key_word HAVING COUNT(key_word) > 1 )t2 WHERE t1.key_word = t2.key_word AND t1.id = t2.minid |
这条语句就跟我们第一条语句是一样的功能,但是这条语句好处就是MIN(id),可以控制是删除大的id重复记录(MAX),还是删除小的id重复记录(MIN)。
OK,经过改良的语句实现了查询,现在就可以利用DELETE FROM语句来删除了。
代码如下 | 复制代码 |
DELETE FROM search_keywrod WHERE id IN (SELECT id FROM ( |
执行时,请多执行几次,因为每次删除时,只删除掉重复的一次记录,如果你一条记录重复五次,那你就要执行五次的了。所以多执行几次,直到没有可删除的记录了,这样你直接一条语句删除掉重复的mysql记录功能就实现了

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
