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

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL 消除重復(fù)行的一些方法

2024-07-24 12:51:46
字體:
供稿:網(wǎng)友

sql語句

/*MySQL 消除重復(fù)行的一些方法---Chu Minfei---2010-08-12 22:49:44.660--引用轉(zhuǎn)載請注明出處:http://blog.csdn.NET/feixianxxx*/----------------全部字段重復(fù)------------------------ --1使用表替換來刪除重復(fù)項 create table test_1(id int,value int); insert test_1 select 1,2 union all select 1,2 union all select 2,3; --建立一個和源表結(jié)構(gòu)一樣的空的臨時表 create table tmp like test_1; --向臨時表插入不重復(fù)的記錄 insert tmp select distinct * from test_1; --刪除原表 drop table test_1; --更改臨時表名為目標表 rename table tmp to test_1; --顯示 mysql> select * from test_1;+------+-------+| id | value |+------+-------+| 1 | 2 || 2 | 3 |+------+-------+ --2.添加auto_increment屬性列(這個方法只能用于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約束 因為有可能列中有NULL值,但是這里NULL就可以多個了.. --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也可以就是上面全部字段去重的第二個方法--4.容易錯誤的方法--有些朋友可能會想到子查詢的方法,我們來試驗一下 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*/ 目前,您不能從一個表中刪除,同時又在子查詢中從同一個表中選擇。 ------------------刪除特定重復(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最小的那個記錄,刪除其他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
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 云阳县| 新巴尔虎右旗| 山丹县| 遂溪县| 胶州市| 桃源县| 米泉市| 醴陵市| 剑河县| 洛浦县| 凤台县| 永嘉县| 巨鹿县| 乌拉特前旗| 光山县| 奉化市| 女性| 于都县| 海城市| 台中县| 景泰县| 山阳县| 池州市| 阳江市| 罗城| 上栗县| 日喀则市| 嘉兴市| 天镇县| 土默特右旗| 绵竹市| 盐边县| 平罗县| 陇南市| 博罗县| 二手房| 个旧市| 五原县| 渝中区| 兴海县| 安宁市|