mysql中重複資料刪除記錄sql語句

來源:互聯網
上載者:User

重複資料刪除記錄方法一:

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:重複的欄位))

重複資料刪除記錄方法四:


具體實現如下:

 代碼如下 複製代碼

Table         Create Table                                           
------------  --------------------------------------------------------
users_groups  CREATE TABLE `users_groups` (                          
                `id` int(10) unsigned NOT NULL AUTO_INCREMENT,       
                `uid` int(11) NOT NULL,                              
                `gid` int(11) NOT NULL,                              
                PRIMARY KEY (`id`)                                   
              ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 

users_groups.txt內容:

1,11,502
2,107,502
3,100,503
4,110,501
5,112,501
6,104,502
7,100,502
8,100,501
9,102,501
10,104,502
11,100,502
12,100,501
13,102,501
14,110,501

mysql> load data infile 'c:\users_groups.txt' into table users_groups fields
terminated by ',' lines terminated by 'n';
Query OK, 14 rows affected (0.05 sec)
Records: 14  Deleted: 0  Skipped: 0  Warnings: 0

mysql> select * from users_groups;

query result(14 records)

id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
7 100 502
8 100 501
9 102 501
10 104 502
11 100 502
12 100 501
13 102 501
14 110 501
14 rows in set (0.00 sec)

根據一位兄弟的建議修改。

 代碼如下 複製代碼

mysql> create temporary table tmp_wrap select * from users_groups group by uid having count(1) >= 1;
Query OK, 7 rows affected (0.11 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> truncate table users_groups;
Query OK, 14 rows affected (0.03 sec)

mysql> insert into users_groups select * from tmp_wrap;
Query OK, 7 rows affected (0.03 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> select * from users_groups;

query result(7 records)
id uid gid
1 11 502
2 107 502
3 100 503
4 110 501
5 112 501
6 104 502
9 102 501


mysql> drop table tmp_wrap;
Query OK, 0 rows affected (0.05 sec)

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

尋找重複的,並且除掉最小的那個。

 代碼如下 複製代碼

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

3、現在來看一下這兩個辦法的效率。
運行一下以下SQL 陳述式

 代碼如下 複製代碼

create index f_uid on users_groups(uid);
explain select * from users_groups group by uid having count(1) > 1 union all
select * from users_groups group by uid having count(1) = 1;
explain select * from  users_groups as a,
(
select *,min(id) from users_groups group by uid having count(1) > 1
) as b
 where a.uid = b.uid and a.id > b.id;
query result(3 records)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY users_groups index (NULL) f_uid 4 (NULL) 14  
2 UNION users_groups index (NULL) f_uid 4 (NULL) 14  
(NULL) UNION RESULT <union1,2> ALL (NULL) (NULL) (NULL) (NULL) (NULL)  

 
query result(3 records)
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <derived2> ALL (NULL) (NULL) (NULL) (NULL) 4  
1 PRIMARY a ref PRIMARY,f_uid f_uid 4 b.uid 1 Using where
2 DERIVED users_groups index (NULL) f_uid 4 (NULL) 14  

 
 


很明顯的第二個比第一個掃描的函數要少。

當沒有建立表或建立索引許可權的時候

建立一個新表,然後將原表中不重複的資料插入新表:

 代碼如下 複製代碼

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.net  |
|  2 | http://111cn.net        |
|  3 | http://www.111cn.net |
|  4 | http://www.111cn.net  |
|  5 | http://www.111cn.net |
+----+------------------------+
5 rows in set (0.00 sec)
 
mysql> select * from demo_new order by id;
+----+------------------------+
| id | site                   |
+----+------------------------+
|  1 | http://www.111cn.net  |
|  2 | http://111cn.net        |
|  3 | http://www.111cn.net |
+----+------------------------+
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.net  |
|  2 | http://111cn.net        |
|  3 | http://www.111cn.net |
+----+------------------------+
3 rows in set (0.00 sec)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.