mysql查詢表裡的重複資料方法和刪除重複資料
mysql查詢表裡的重複資料方法:
1
2
3
4
|
INSERT INTO hk_test(username, passwd) VALUES ( 'qmf1' , 'qmf1' ),( 'qmf2' , 'qmf11' ) delete from hk_test where username= 'qmf1' and passwd= 'qmf1' |
MySQL裡查詢表裡的重複資料記錄:
先檢視重複的原始資料:
場景一:列出username欄位有重讀的資料
1
2
3
|
select username,count(*) as count from hk_test group by username having count> 1 ; SELECT username,count(username) as count FROM hk_test GROUP BY username HAVING count(username)
> 1 ORDER BY count DESC; |
這種方法只是統計了該欄位重複對應的具體的個數
場景二:列出username欄位重複記錄的具體指:
1
2
3
4
5
|
select * from hk_test where username in (select username from hk_test group by username having
count(username) > 1 ) SELECT username,passwd FROM hk_test WHERE username in ( SELECT username FROM hk_test GROUP BY username HAVING count(username)> 1 ) 但是這條語句在mysql中效率太差,感覺mysql並沒有為子查詢生成臨時表。在資料量大的時候,耗時很長時間 |
解決方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
於是使用先建立臨時表 複製程式碼 程式碼如下: create table `tmptable` as ( SELECT `name` FROM `table` GROUP BY `name` HAVING count(`name`) > 1 ); 然後使用多表連線查詢 複製程式碼 程式碼如下: SELECT a.`id`, a.`name` FROM `table` a, `tmptable` t WHERE a.`name` = t.`name`; 結果這次結果很快就出來了。 用 distinct去重複 複製程式碼 程式碼如下: SELECT distinct a.`id`, a.`name` FROM `table` a, `tmptable` t WHERE a.`name` = t.`name`; |
1
2
|
select * from hk_test a where (a.username,a.passwd) in (select username,passwd from hk_test group by username,passwd having count(*) > 1 ) |
場景四:查詢表中多個欄位同時重複的記錄:
1
|
select username,passwd,count(*) from hk_test group by username,passwd having count(*) > 1 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
MySQL查詢表內重複記錄 查詢及刪除重複記錄的方法 (一) 1 、查詢表中多餘的重複記錄,重複記錄是根據單個欄位(peopleId)來判斷 select * from people where peopleId in (select peopleId from people group by peopleId having count(peopleId)> 1 ) 2 、刪除表中多餘的重複記錄,重複記錄是根據單個欄位(peopleId)來判斷,只留有一個記錄 delete from people where peopleId in (select peopleId from people group by peopleId having count(peopleId)> 1 ) and min(id) not in (select id 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 ) 5 、查詢表中多餘的重複記錄(多個欄位),不包含rowid最小的記錄 select * 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 ) (二) 比方說 在A表中存在一個欄位“name”,而且不同記錄之間的“name”值有可能會相同,現在就是需要查詢出在該表中的各記錄之間,“name”值存在重複的項; Select Name,Count(*) From A Group By Name Having Count(*) > 1 如果還查性別也相同大則如下: Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1 (三) 方法一 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 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
SELECT * from tab1 where CompanyName in( SELECT companyname from tab1 GROUP BY CompanyName HAVING COUNT(*)> 1 ); -- 129 .433ms SELECT * from tab1 INNER join ( SELECT companyname from tab1 GROUP BY CompanyName HAVING COUNT(*)> 1 ) as tab2 USING(CompanyName); -- 0 .482ms 方法二 有兩個意義上的重複記錄,一是完全重複的記錄,也即所有欄位均重複的記錄,二是部分關鍵欄位重複的記錄,比如Name欄位重複,而其他欄位不一定重複或都重複可以忽略。 1 、對於第一種重複,比較容易解決,使用 select distinct * from tableName 就可以得到無重複記錄的結果集。 如果該表需要刪除重複的記錄(重複記錄保留 1 條),可以按以下方法刪除 select distinct * into #Tmp from tableName drop table tableName select * into tableName from #Tmp drop table #Tmp 發生這種重複的原因是表設計不周產生的,增加唯一索引列即可解決。 2 、這類重複問題通常要求保留重複記錄中的第一條記錄,操作方法如下 假設有重複的欄位為Name,Address,要求得到這兩個欄位唯一的結果集 select identity( int , 1 , 1 ) as autoID, * into #Tmp from tableName select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID select * from #Tmp where autoID in(select autoID from #tmp2) 最後一個select即得到了Name,Address不重複的結果集(但多了一個autoID欄位,實際寫時可以寫在select子句中省去此列) (四)查詢重複 select * from tablename where id in ( select id from tablename group by id having count(id) > 1 ) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
常用的語句 1 、查詢表中多餘的重複記錄,重複記錄是根據單個欄位(mail_id)來判斷 程式碼如下 複製程式碼 SELECT * FROM table WHERE mail_id IN (SELECT mail_id FROM table GROUP BY mail_id HAVING COUNT(mail_id) > 1 ); 2 、刪除表中多餘的重複記錄,重複記錄是根據單個欄位(mail_id)來判斷,只留有rowid最小的記錄 程式碼如下 複製程式碼 DELETE FROM table WHERE mail_id IN (SELECT mail_id FROM table GROUP BY mail_id HAVING COUNT(mail_id) > 1 ) AND rowid NOT IN (SELECT MIN(rowid) FROM table GROUP BY mail_id HAVING COUNT(mail_id )> 1 ); 3 、查詢表中多餘的重複記錄(多個欄位) 程式碼如下 複製程式碼 SELECT * FROM table WHERE (mail_id,phone) IN (SELECT mail_id,phone FROM table GROUP BY mail_id,phone HAVING COUNT(*) > 1 ); 4 、刪除表中多餘的重複記錄(多個欄位),只留有rowid最小的記錄 程式碼如下 複製程式碼 DELETE FROM table WHERE (mail_id,phone) IN (SELECT mail_id,phone FROM table GROUP BY mail_id,phone HAVING COU(www.111cn.net)NT(*) > 1 ) AND rowid NOT IN (SELECT MIN(rowid) FROM table GROUP
BY mail_id,phone HAVING COUNT(*)> 1 ); 5 、查詢表中多餘的重複記錄(多個欄位),不包含rowid最小的記錄 程式碼如下 複製程式碼 SELECT * FROM table WHERE (a.mail_id,a.phone) IN (SELECT mail_id,phone FROM table GROUP BY mail_id,phone HAVING COUNT(*) > 1 ) AND rowid NOT IN (SELECT MIN(rowid) FROM table GROUP BY mail_id,phone HAVING COUNT(*)> 1 ); 儲存過程 程式碼如下 複製程式碼 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 (一)單個欄位 1 、查詢表中多餘的重複記錄,根據(question_title)欄位來判斷 程式碼如下 複製程式碼 select * from questions where question_title in (select question_title from people group by question_title having count(question_title) > 1 ) 2 、刪除表中多餘的重複記錄,根據(question_title)欄位來判斷,只留有一個記錄 程式碼如下 複製程式碼 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 ) (二)多個欄位 刪除表中多餘的重複記錄(多個欄位),只留有rowid最小的記錄 程式碼如下 複製程式碼 DELETE FROM questions WHERE (questions_title,questions_scope) IN (SELECT questions_title,questions_scope FROM que(www.111cn.net)stions 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 ) 用上述語句無法刪除,建立了臨時表才刪的,求各位達人解釋一下。 程式碼如下 複製程式碼 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; |
查詢mysql資料表中重複記錄
mysql資料庫中的資料越來越多,當然排除不了重複的資料,在維護資料的時候突然想到要把多餘的資料給刪減掉,剩下有價值的資料。
以下sql語句可以實現查詢出一個表中的所有重複的記錄.
select user_name,count(*) as count from user_table group by user_name having count>1;
引數說明:
user_name為要查詢的重複欄位.
count用來判斷大於一的才是重複的.
user_table為要查詢的表名.
group by用來分組
having用來過濾.
把引數換成自己資料表的相應欄位引數,可以先在Phpmyadmin裡面或者Navicat裡面去執行,看看有哪些資料重複了,然後在資料庫裡面刪除掉,也可以直接將SQL語句放到後臺讀取新聞的頁面裡面讀取出來,完善成查詢重複資料的列表,有重複的可以直接刪除。
效果如下:
缺點:這種方法的缺點就是當你的資料庫裡面的資料量很大的時候,效率很低,我用的是Navicat測試的,資料量不大,效率很高,當然,網站還有其它查詢資料重複的SQL語句,舉一反三,大家好好研究研究,找到一個適合自己網站的查詢語句。
相關文章
- mysql 查詢及 刪除表中重複資料MySql
- 刪除表裡重複資料
- Oracle查詢重複資料與刪除重複記錄方法Oracle
- Oracle查詢重複資料與刪除重複記錄Oracle
- oracle 查詢及刪除表中重複資料Oracle
- mysql 刪除表中重複的資料MySql
- MySQL刪除重複資料MySql
- oracle重複資料的查詢及刪除Oracle
- MySQL 查詢重複的資料MySql
- 刪除重複資料
- PostgreSQL刪除表中重複資料SQL
- mysql連表查詢出現資料重複MySql
- 重複資料刪除和SSD的互補方法
- mongodb刪除重複資料MongoDB
- 【常用方法推薦】如何刪除MySQL的重複資料?MySql
- Oracle中刪除表中的重複資料Oracle
- Mongodb 刪除重複資料的幾個方法MongoDB
- oracle 刪除重複資料的幾種方法Oracle
- 刪除重複資料的幾個方法(轉)
- sqlserver中刪除重複資料SQLServer
- sql查詢一張表的重複資料SQL
- mysql表刪除重複記錄方法MySql
- 查詢刪除表中重複記錄
- 處理表重複記錄(查詢和刪除)
- 刪除重複資料的一種高效的方法
- 解析postgresql 刪除重複資料案例SQL
- mysql 查詢出重複資料的第一條MySql
- Oracle中刪除重複資料的SqlOracleSQL
- excel刪除重複資料保留一條 如何刪掉重複資料只留一條Excel
- SQL Server中刪除重複資料的幾個方法SQLServer
- MySQL資料庫行去重複和列去重複MySql資料庫
- excel 查詢重複資料,且能提示那行與那行重複Excel
- MS SQL Server 刪除重複行資料SQLServer
- T-SQL 刪除重複資料SQLSQL
- 海量資料處理_刪除重複行
- 根據rowid刪除重複資料
- 通過ROWID刪除重複資料
- 查詢/刪除重複的資料(單個欄位和多個欄位條件)