共計(jì) 2825 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。
自動(dòng)寫代碼機(jī)器人,免費(fèi)開通
這篇文章給大家分享的是有關(guān) MySQL 如何消除重復(fù)行的內(nèi)容。丸趣 TV 小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨丸趣 TV 小編過來看看吧。
sql 語句
/*
MySQL 消除重復(fù)行的一些方法
---Chu Minfei
---2010-08-12 22:49:44.660
-- 引用轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.NET/feixianxxx
---------------- 全部字段重復(fù) ------------------------
-- 1 使用表替換來刪除重復(fù)項(xiàng)
create table test_1(id int,value int);
insert test_1 select 1,2 union all select 1,2 union all select 2,3;
-- 建立一個(gè)和源表結(jié)構(gòu)一樣的空的臨時(shí)表
create table tmp like test_1;
-- 向臨時(shí)表插入不重復(fù)的記錄
insert tmp select distinct * from test_1;
-- 刪除原表
drop table test_1;
-- 更改臨時(shí)表名為目標(biāo)表
rename table tmp to test_1;
-- 顯示
mysql select * from test_1;
+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
| 2 | 3 |
+------+-------+
--2. 添加 auto_increment 屬性列 (這個(gè)方法只能用于 MyISAM 或者 BDB 引擎的表)
create table test_1(id int,value int) engine=MyISAM;
insert test_1 select 1,2 union all select 1,2 union all select 2,3;
alter table test_1 add id2 int not null auto_increment,
add primary key(id,value,id2);
select * from test_1;
+----+-------+-----+
| id | value | id2 |
+----+-------+-----+
| 1 | 2 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 1 |
+----+-------+-----+
delete from test_1 where id2 1;
alter table test_1 drop id2;
select * from test_1;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 3 |
+----+-------+
------------------- 部分字段重復(fù) ---------------------
--1. 加索引的方式
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
Alter IGNORE table test_2 add primary key(id);
select * from test_2;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 3 |
+----+-------+
我們可以看到 1 3 這條記錄消失了
我們這里也可以使用 Unique 約束 因?yàn)橛锌赡芰兄杏?NULL 值,但是這里 NULL 就可以多個(gè)了..
--2. 聯(lián)合表刪除
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
delete A from test_2 a join (select MAX(value) as v ,ID from test_2 group by id) b
on a.id=b.id and a.value b.v;
select * from test_2;
+------+-------+
| id | value |
+------+-------+
| 1 | 3 |
| 2 | 3 |
+------+-------+
--3. 使用 Increment_auto 也可以就是上面全部字段去重的第二個(gè)方法
--4. 容易錯(cuò)誤的方法
-- 有些朋友可能會(huì)想到子查詢的方法,我們來試驗(yàn)一下
create table test_2(id int,value int);
insert test_2 select 1,2 union all select 1,3 union all select 2,3;
delete a from test_2 a where exists(select * from test_2 where a.id=id and a.value value);
/*ERROR 1093 (HY000): You can t specify target table a for update in FROM clause*/
目前,您不能從一個(gè)表中刪除,同時(shí)又在子查詢中從同一個(gè)表中選擇。
------------------ 刪除特定重復(fù)行 --------------
-- 主要通過 order by +limit 或者直接 limit
create table test_3(id int,value int);
insert test_3 select 1,2 union all select 1,3 union all select 1,4 union all select 2,3;
-- 這是要保留 ID=1 value 最小的那個(gè)記錄,刪除其他 id 為的記錄
delete from test_3 where id=1 order by value desc limit 2;
select * from test_3;
+------+-------+
| id | value |
+------+-------+
| 1 | 2 |
| 2 | 3 |
+------+-------+
如果你只想刪除任意的記錄 保留一條 就可以去掉 order by
感謝各位的閱讀!關(guān)于“MySQL 如何消除重復(fù)行”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
向 AI 問一下細(xì)節(jié)
正文完