日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

Mysql中查找并删除重复数据的方法

發(fā)布時(shí)間:2023/12/18 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mysql中查找并删除重复数据的方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

(一)單個(gè)字段

1、查找表中多余的重復(fù)記錄,根據(jù)(question_title)字段來(lái)判斷

?代碼如下 復(fù)制代碼
select * from questions where question_title in (select question_title from people group by question_title having count(question_title) > 1)
?

2、刪除表中多余的重復(fù)記錄,根據(jù)(question_title)字段來(lái)判斷,只留有一個(gè)記錄

?代碼如下 復(fù)制代碼
delete from questions
where peopleId in (select peopleId from people group by peopleId having count(question_title) > 1)
and min(id) not in (select question_id from questions group by question_title having count(question_title)>1)

(二)多個(gè)字段

刪除表中多余的重復(fù)記錄(多個(gè)字段),只留有rowid最小的記錄

?代碼如下 復(fù)制代碼
DELETE FROM questions WHERE (questions_title,questions_scope) IN (SELECT questions_title,questions_scope FROM questions GROUP BY questions_title,questions_scope HAVING COUNT(*) > 1) AND question_id NOT IN (SELECT MIN(question_id) FROM questions GROUP BY questions_scope,questions_title HAVING COUNT(*)>1)
?

用上述語(yǔ)句無(wú)法刪除,創(chuàng)建了臨時(shí)表才刪的,求各位達(dá)人解釋一下。

?代碼如下 復(fù)制代碼
CREATE TABLE tmp AS SELECT question_id FROM questions WHERE (questions_title,questions_scope) IN (SELECT questions_title,questions_scope FROM questions GROUP BY questions_title,questions_scope HAVING COUNT(*) > 1) AND question_id NOT IN (SELECT MIN(question_id) FROM questions GROUP BY questions_scope,questions_title HAVING COUNT(*)>1);

DELETE FROM questions WHERE question_id IN (SELECT question_id FROM tmp);

DROP TABLE tmp;
?

(三) 存儲(chǔ)過(guò)程

?代碼如下 復(fù)制代碼
declare @max integer,@id integer

declare cur_rows cursor local for select 主字段,count(*) from 表名 group by 主字段 having count(*) >; 1

open cur_rows

fetch cur_rows into @id,@max

while @@fetch_status=0

begin

select @max = @max -1

set rowcount @max

delete from 表名 where 主字段 = @id

fetch cur_rows into @id,@max

end

close cur_rows

set rowcount 0
?

例,

數(shù)據(jù)庫(kù)版本 Server version: 5.1.41-community-log MySQL Community Server (GPL)

例1,表中有主鍵(可唯一標(biāo)識(shí)的字段),且該字段為數(shù)字類型

例1測(cè)試數(shù)據(jù)

?代碼如下 復(fù)制代碼
/* 表結(jié)構(gòu) */
DROP TABLE IF EXISTS `t1`;
CREATE TABLE IF NOT EXISTS `t1`(
? `id` INT(1) NOT NULL AUTO_INCREMENT,
? `name` VARCHAR(20) NOT NULL,
? `add` VARCHAR(20) NOT NULL,
? PRIMARY KEY(`id`)
)Engine=InnoDB;

/* 插入測(cè)試數(shù)據(jù) */
INSERT INTO `t1`(`name`,`add`) VALUES
('abc',"123"),
('abc',"123"),
('abc',"321"),
('abc',"123"),
('xzy',"123"),
('xzy',"456"),
('xzy',"456"),
('xzy',"456"),
('xzy',"789"),
('xzy',"987"),
('xzy',"789"),
('ijk',"147"),
('ijk',"147"),
('ijk',"852"),
('opq',"852"),
('opq',"963"),
('opq',"741"),
('tpk',"741"),
('tpk',"963"),
('tpk',"963"),
('wer',"546"),
('wer',"546"),
('once',"546");

SELECT * FROM `t1`;
+----+------+-----+
| id | name | add |
+----+------+-----+
|? 1 | abc? | 123 |
|? 2 | abc? | 123 |
|? 3 | abc? | 321 |
|? 4 | abc? | 123 |
|? 5 | xzy? | 123 |
|? 6 | xzy? | 456 |
|? 7 | xzy? | 456 |
|? 8 | xzy? | 456 |
|? 9 | xzy? | 789 |
| 10 | xzy? | 987 |
| 11 | xzy? | 789 |
| 12 | ijk? | 147 |
| 13 | ijk? | 147 |
| 14 | ijk? | 852 |
| 15 | opq? | 852 |
| 16 | opq? | 963 |
| 17 | opq? | 741 |
| 18 | tpk? | 741 |
| 19 | tpk? | 963 |
| 20 | tpk? | 963 |
| 21 | wer? | 546 |
| 22 | wer? | 546 |
| 23 | once | 546 |
+----+------+-----+
rows in set (0.00 sec)
?

查找id最小的重復(fù)數(shù)據(jù)(只查找id字段)

?代碼如下 復(fù)制代碼
/* 查找id最小的重復(fù)數(shù)據(jù)(只查找id字段) */
SELECT DISTINCT MIN(`id`) AS `id`
FROM `t1`
GROUP BY `name`,`add`
HAVING COUNT(1) > 1;
+------+
| id?? |
+------+
|??? 1 |
|?? 12 |
|?? 19 |
|?? 21 |
|??? 6 |
|??? 9 |
+------+
rows in set (0.00 sec)
?


查找所有重復(fù)數(shù)據(jù)

?代碼如下 復(fù)制代碼
/* 查找所有重復(fù)數(shù)據(jù) */
SELECT `t1`.*
FROM `t1`,(
? SELECT `name`,`add`
? FROM `t1`
? GROUP BY `name`,`add`
? HAVING COUNT(1) > 1
) AS `t2`
WHERE `t1`.`name` = `t2`.`name`
? AND `t1`.`add` = `t2`.`add`;
+----+------+-----+
?

| id | name | add |
+----+------+-----+
|? 1 | abc? | 123 |
|? 2 | abc? | 123 |
|? 4 | abc? | www.111cn.net|
|? 6 | xzy? | 456 |
|? 7 | xzy? | 456 |
|? 8 | xzy? | 456 |
|? 9 | xzy? | 789 |
| 11 | xzy? | 789 |
| 12 | ijk? | 147 |
| 13 | ijk? | 147 |
| 19 | tpk? | 963 |
| 20 | tpk? | 963 |
| 21 | wer? | 546 |
| 22 | wer? | 546 |
+----+------+-----+
rows in set (0.00 sec)

更多詳細(xì)內(nèi)容請(qǐng)查看:http://www.111cn.net/database/mysql/56725.htm

轉(zhuǎn)載于:https://www.cnblogs.com/alibai/p/3510061.html

總結(jié)

以上是生活随笔為你收集整理的Mysql中查找并删除重复数据的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。