国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql中刪除重復記錄sql語句

2024-07-24 12:38:22
字體:
來源:轉載
供稿:網友

在sql中我們經常會碰到有重復的一些數據,下面我來介紹在mysql中刪除重復記錄的多種方法,有需要的朋友可參考參考.

刪除重復記錄方法一:

1.新建一個臨時表,代碼如下:

create table tmp as select * from youtable group by  name(name為不希望有重復的列)

2.刪除原來的表,代碼如下:drop table youtable

3.重命名表,代碼如下:

alter table tmp rename youtable

但是這個方法有個問題,由臨時表轉變過來的最終表,其表結構會和原來的不一致,需要手工更改。這個問題,待解決。

刪除重復記錄方法二:

1.新建一個臨時表,代碼如下:

CREATE TABLE tmp AS SELECT * FROM youtable GROUP BY name(name為不希望有重復的列)

2.清空原來的表,代碼如下:

TRUNCATE TABLE youtable

3.把臨時表插入到youtable,代碼如下:

INSERT INTO tablename SELECT  * FROM temp

4.刪除臨時表,代碼如下:

DROP TABLE  temp

刪除重復記錄方法三:代碼如下:

delete table where ID not in(select min(ID) from table group by name(name:重復的字段))

刪除重復記錄方法四:

具體實現如下:

  1. Table         Create Table                                             
  2. ------------  -------------------------------------------------------- 
  3. users_groups  CREATE TABLE `users_groups` (                            
  4.                 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,         
  5.                 `uid` int(11) NOT NULL,                                
  6.                 `gid` int(11) NOT NULL,                                
  7.                 PRIMARY KEY (`id`)                                     
  8.               ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8  

users_groups.txt內容:

  1. 1,11,502 
  2. 2,107,502 
  3. 3,100,503 
  4. 4,110,501 
  5. 5,112,501 
  6. 6,104,502 
  7. 7,100,502 
  8. 8,100,501 
  9. 9,102,501 
  10. 10,104,502 
  11. 11,100,502 
  12. 12,100,501 
  13. 13,102,501 
  14. 14,110,501 
  15.  
  16. mysql> load data infile 'c:/users_groups.txt' into table users_groups fields 
  17. terminated by ',' lines terminated by 'n'
  18. Query OK, 14 rows affected (0.05 sec) 
  19. Records: 14  Deleted: 0  Skipped: 0  Warnings: 0 
  20.  
  21. mysql> select * from users_groups; 
  22.  
  23. query result(14 records) 
  24.  
  25. id uid gid  
  26. 1 11 502  
  27. 2 107 502  
  28. 3 100 503  
  29. 4 110 501  
  30. 5 112 501  
  31. 6 104 502  
  32. 7 100 502  
  33. 8 100 501  
  34. 9 102 501  
  35. 10 104 502  
  36. 11 100 502  
  37. 12 100 501  
  38. 13 102 501  
  39. 14 110 501  
  40. 14 rows in set (0.00 sec) 

根據一位兄弟的建議修改,代碼如下:

  1. mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) >= 1; 
  2. Query OK, 7 rows affected (0.11 sec) 
  3. Records: 7  Duplicates: 0  Warnings: 0 
  4.  
  5. mysql> truncate table users_groups; 
  6. Query OK, 14 rows affected (0.03 sec) 
  7.  
  8. mysql> insert into users_groups select * from tmp_wrap; 
  9. Query OK, 7 rows affected (0.03 sec) 
  10. Records: 7  Duplicates: 0  Warnings: 0 
  11.  
  12. mysql> select * from users_groups; 
  13. --Vevb.com 
  14. query result(7 records) 
  15. id uid gid  
  16. 1 11 502  
  17. 2 107 502  
  18. 3 100 503  
  19. 4 110 501  
  20. 5 112 501  
  21. 6 104 502  
  22. 9 102 501 
  23.  
  24. mysql> drop table tmp_wrap; 
  25. Query OK, 0 rows affected (0.05 sec) 

2、還有一個很精簡的辦法.

查找重復的,并且除掉最小的那個,代碼如下:

  1. delete users_groups as a from users_groups as a, 
  2. select *,min(id) from users_groups group by uid having count(1) > 1 
  3. as b 
  4.  where a.uid = b.uid and a.id > b.id; 
  5. (7 row(s)affected) 
  6. (0 ms taken) 
  7.  
  8. query result(7 records) 
  9. id uid gid  
  10. 1 11 502  
  11. 2 107 502  
  12. 3 100 503  
  13. 4 110 501  
  14. 5 112 501  
  15. 6 104 502  
  16. 9 102 501 

3、現在來看一下這兩個辦法的效率,運行一下以下SQL 語句,代碼如下:

  1. create index f_uid on users_groups(uid); 
  2. explain select * from users_groups group by uid having count(1) > 1 union all 
  3. select * from users_groups group by uid having count(1) = 1; 
  4. explain select * from  users_groups as a, 
  5. select *,min(id) from users_groups group by uid having count(1) > 1 
  6. as b 
  7.  where a.uid = b.uid and a.id > b.id; 
  8. query result(3 records) 
  9. id select_type table type possible_keys key key_len ref rows Extra  
  10. PRIMARY users_groups index (NULL) f_uid 4 (NULL) 14    
  11. UNION users_groups index (NULL) f_uid 4 (NULL) 14    
  12. (NULLUNION RESULT <union1,2> ALL (NULL) (NULL) (NULL) (NULL) (NULL)   
  13.  
  14.  
  15. query result(3 records) 
  16. id select_type table type possible_keys key key_len ref rows Extra  
  17. PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 4    
  18. PRIMARY a ref PRIMARY,f_uid f_uid 4 b.uid 1 Using where  
  19. 2 DERIVED users_groups index (NULL) f_uid 4 (NULL) 14   

很明顯的第二個比第一個掃描的函數要少,當沒有創建表或創建索引權限的時候,創建一個新表,然后將原表中不重復的數據插入新表,代碼如下:

  1. mysql> create table demo_new as select * from demo group by site;  
  2. Query OK, 3 rows affected (0.19 sec)  
  3. Records: 3  Duplicates: 0  Warnings: 0  
  4.    
  5. mysql> show tables;  
  6. +----------------+  
  7. | Tables_in_test |  
  8. +----------------+  
  9. | demo           |  
  10. | demo_new       |  
  11. +----------------+  
  12. rows in set (0.00 sec)  
  13.    
  14. mysql> select * from demo order by id;  
  15. +----+------------------------+  
  16. | id | site                   |  
  17. +----+------------------------+  
  18. |  1 | http://m.survivalescaperooms.com  |  
  19. |  2 | http://Vevb.com        |  
  20. |  3 | http://m.survivalescaperooms.com |  
  21. |  4 | http://m.survivalescaperooms.com  |  
  22. |  5 | http://m.survivalescaperooms.com |  
  23. +----+------------------------+  
  24. rows in set (0.00 sec)  
  25.    
  26. mysql> select * from demo_new order by id;  
  27. +----+------------------------+  
  28. | id | site                   |  
  29. +----+------------------------+  
  30. |  1 | http://m.survivalescaperooms.com  |  
  31. |  2 | http://Vevb.com        |  
  32. |  3 | http://m.survivalescaperooms.com |  
  33. +----+------------------------+  
  34. rows in set (0.00 sec) 

然后將原表備份,將新表重命名為當前表,代碼如下:

  1. mysql> rename table demo to demo_old, demo_new to demo;  
  2. Query OK, 0 rows affected (0.04 sec)  
  3.    
  4. mysql> show tables;  
  5. +----------------+  
  6. | Tables_in_test |  
  7. +----------------+  
  8. | demo           |  
  9. | demo_old       |  
  10. +----------------+  
  11. rows in set (0.00 sec)  
  12.    
  13. mysql> select * from demo order by id;  
  14. +----+------------------------+  
  15. | id | site                   |  
  16. +----+------------------------+  
  17. |  1 | http://m.survivalescaperooms.com  |  
  18. |  2 | http://Vevb.com        |  
  19. |  3 | http://m.survivalescaperooms.com |  
  20. +----+------------------------+  
  21. rows in set (0.00 sec)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀安县| 连城县| 阿荣旗| 桃江县| 哈密市| 海兴县| 罗定市| 武宁县| 兰州市| 句容市| 云林县| 普兰店市| 永仁县| 天水市| 沾益县| 喀什市| 那坡县| 姚安县| 庆云县| 商都县| 松江区| 温宿县| 兴城市| 弥勒县| 武邑县| 富宁县| 孟村| 霸州市| 南和县| 平山县| 鄂伦春自治旗| 南漳县| 那坡县| 平阴县| 鄄城县| 花莲县| 阿勒泰市| 友谊县| 东阿县| 丰城市| 沁水县|