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

來源:互聯網
上載者:User

建立一個表用來存放,要刪除的記錄的id資訊:

 代碼如下 複製代碼

CREATE TABLE `tmp_ids` (
  `id` int(11),
  `name` char(20)  
) ENGINE=MyISAM;

如果要刪除的記錄不多的話,可以把這個表建立成記憶體表形式:

 代碼如下 複製代碼

CREATE TABLE `tmp_ids` (
  `id` int(11),
  `name` char(20) 
) ENGINE=HEAP;

然後在test表中重複資料刪除記錄:

 代碼如下 複製代碼

insert into tmp_ids select min(id),name from test group by name having count(*)>1 order by null;
delete a.* from test a,tmp_ids b where b.name=a.name and a.id>b.id;
truncate table tmp_ids;

方法二

複製無重複記錄到新表格,刪除舊錶格,然後重新命名新表格為舊錶名稱。

 代碼如下 複製代碼
 
mysql> select * from duplicate where id in(select min(id) from duplicate group by name);
+----+-------+
| id | name  |
+----+-------+
|  1 | wang  |
|  3 | wdang |
|  5 | wdand |
|  6 | wddda |
+----+-------+
4 rows in set (0.01 sec)
mysql> create table duplica select * from duplicate where id in(select min(id) from duplicate group by name);
Query OK, 4 rows affected (0.02 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> drop table duplicate;
Query OK, 0 rows affected (0.01 sec)
mysql> alter table duplica rename to duplicate;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from duplicate;
+----+-------+
| id | name  |
+----+-------+
|  1 | wang  |
|  3 | wdang |
|  5 | wdand |
|  6 | wddda |
+----+-------+
4 rows in set (0.00 sec)
 
mysql> alter table duplicate modify id int(2) not null primary key auto_increment;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

 
 
後來想了一個語句搞定了:

 代碼如下 複製代碼
mysql> use mysql
Database changed
mysql> select * from duplicate;
+----+-------+
| id | name  |
+----+-------+
|  1 | wang  |
|  3 | wdang |
|  5 | wdand |
|  6 | wddda |
|  2 | wang  |
|  4 | wdang |
+----+-------+
6 rows in set (0.00 sec)
mysql> delete duplicate as a from duplicate as a,
    -> (
    -> select * from duplicate group by name having count(1)>1) as b
    -> where a.name=b.name and a.id > b.id;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from duplicate;
+----+-------+
| id | name  |
+----+-------+
|  1 | wang  |
|  3 | wdang |
|  5 | wdand |
|  6 | wddda |
+----+-------+
4 rows in set (0.00 sec)

保留ID最小的記錄。

第1種:

 代碼如下 複製代碼
delete from %s where goodsurl in (select goodsrul as gurl1 from %s
         #group by grul1 having count(gurl1)>1)rs1 and id not in (select min(id)as id2 from %s
          #group by goodsurl having count(goodsurl)>1)rs2"%(a,a,a)

第2種:這種方法不使用子集,但是我不知道怎麼把rs1,rs2裡面的元組嵌入到SQL語句中,

 代碼如下 複製代碼

exeSql = "select min(id) from %s group by goodsurl havingcount(goodsurl)>1)"%(a,)
cur.execute(exeSql)
rs1 = cur.fetchall()
exeSql = "select goodsurl from %s group by goodsurl havingcount(goodsurl)>1"%(a,)
cur.execute(exeSql)
rs2 = cur.fetchall()
exeSql = "delete from %s where goodsurl in %s and id not in %"%(a,rs2,rs1)
cur.execute(exeSql)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.