国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

MySQL消除重復(fù)行方法分享

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-09 08:44:38
文檔

MySQL消除重復(fù)行方法分享

MySQL消除重復(fù)行方法分享:本文主要介紹了MySQL 消除重復(fù)行的一些方法,需要的朋友可以參考下,希望能幫助到大家。sql語句/* MySQL 消除重復(fù)行的一些方法 ---Chu Minfei ---2010-08-12 22:49:44.660 --引用轉(zhuǎn)載請注明出處:http://blog.csdn.NET/feixianx
推薦度:
導(dǎo)讀MySQL消除重復(fù)行方法分享:本文主要介紹了MySQL 消除重復(fù)行的一些方法,需要的朋友可以參考下,希望能幫助到大家。sql語句/* MySQL 消除重復(fù)行的一些方法 ---Chu Minfei ---2010-08-12 22:49:44.660 --引用轉(zhuǎn)載請注明出處:http://blog.csdn.NET/feixianx

本文主要介紹了MySQL 消除重復(fù)行的一些方法,需要的朋友可以參考下,希望能幫助到大家。

sql語句

/*
MySQL 消除重復(fù)行的一些方法
---Chu Minfei
---2010-08-12 22:49:44.660
--引用轉(zhuǎn)載請注明出處: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;
 --建立一個和源表結(jié)構(gòu)一樣的空的臨時表
 create table tmp like test_1;
 --向臨時表插入不重復(fù)的記錄
 insert tmp select distinct * from test_1;
 --刪除原表
 drop table test_1;
 --更改臨時表名為目標(biāo)表
 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約束 因?yàn)橛锌赡芰兄杏蠳ULL值,但是這里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.容易錯誤的方法
--有些朋友可能會想到子查詢的方法,我們來試驗(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*/
 
 目前,您不能從一個表中刪除,同時又在子查詢中從同一個表中選擇。
 
 
 ------------------刪除特定重復(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

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

MySQL消除重復(fù)行方法分享

MySQL消除重復(fù)行方法分享:本文主要介紹了MySQL 消除重復(fù)行的一些方法,需要的朋友可以參考下,希望能幫助到大家。sql語句/* MySQL 消除重復(fù)行的一些方法 ---Chu Minfei ---2010-08-12 22:49:44.660 --引用轉(zhuǎn)載請注明出處:http://blog.csdn.NET/feixianx
推薦度:
標(biāo)簽: 分享 方法 重復(fù)
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 亚洲午夜久久久久久91 | 国产淫视频 | 日韩在线电影 | 欧美一区二区在线观看视频 | 亚洲最大色网 | 中文字幕日韩欧美 | 欧美 韩国 精品 另类 综合 | 最近免费中文字幕大全免费版视频 | 亚洲专区欧美 | 国产成人高清一区二区私人 | 欧美大色 | 久久久久九九 | 免费视频一区二区 | 视频在线观看国产 | 日韩欧美国产精品第一页不卡 | 欧美国产日韩在线观看 | 国产国语在线播放视频 | 国产日韩欧美中文 | 国产一区二区免费视频 | 日本韩国欧美一区 | 亚洲一区自拍 | 国产精品网站在线观看 | 日韩中文字幕第一页 | 中文字幕第7页 | 国产精品大全国产精品 | 国产精品久久久久久久久久久久 | 欧美日韩中文 | xxxwww欧美性| 国产美女啪啪 | 日韩区在线 | 经典三级第一页 | 欧美一级色图 | 伊人色播| 欧美交配 | 麻豆porn | 综合精品欧美日韩国产在线 | 99热91| 国产成人精品免费大全 | 亚洲欧美综合网 | 免费一看一级毛片人 | 久久精品亚洲一区二区 |