欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年05月20日 13:55:53   投稿:mdxy-dxy  
這篇文章主要介紹了MySQL 消除重復(fù)行的一些方法,需要的朋友可以參考下

sql語(yǔ)句

/*
MySQL 消除重復(fù)行的一些方法
---Chu Minfei
---2010-08-12 22:49:44.660
--引用轉(zhuǎn)載請(qǐng)注明出處:http://blog.csdn.NET/feixianxxx
*/
----------------全部字段重復(fù)------------------------
 --1使用表替換來(lái)刪除重復(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)橛锌赡芰兄杏蠳ULL值,但是這里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ì)想到子查詢的方法,我們來(lái)試驗(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ù)行--------------
 --主要通過(guò)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將表名稱修改成大寫(xiě)的存儲(chǔ)過(guò)程

    MYSQL將表名稱修改成大寫(xiě)的存儲(chǔ)過(guò)程

    這篇文章主要為大家詳細(xì)介紹了MYSQL將表名稱修改成大寫(xiě)的存儲(chǔ)過(guò)程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • MySQL 5.7 版本的安裝及簡(jiǎn)單使用(圖文教程)

    MySQL 5.7 版本的安裝及簡(jiǎn)單使用(圖文教程)

    這篇文章主要介紹了MySQL 5.7 版本的安裝及簡(jiǎn)單使用(圖文教程)的相關(guān)資料,這里對(duì)mysql 5.7的安裝及使用和注意事項(xiàng),需要的朋友可以參考下
    2016-12-12
  • MySQL 修改數(shù)據(jù)庫(kù)名稱的一個(gè)新奇方法

    MySQL 修改數(shù)據(jù)庫(kù)名稱的一個(gè)新奇方法

    這篇文章主要介紹了MySQL 修改數(shù)據(jù)庫(kù)名稱的一個(gè)新奇方法,MySQL 修改數(shù)據(jù)庫(kù)名的一個(gè)變通方法,需要的朋友可以參考下
    2014-07-07
  • Mysql中count(*)、count(1)、count(主鍵id)與count(字段)的區(qū)別

    Mysql中count(*)、count(1)、count(主鍵id)與count(字段)的區(qū)別

    本文主要介紹了Mysql中count(*)、count(1)、count(主鍵id)與count(字段)的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 軟件測(cè)試-MySQL(六:數(shù)據(jù)庫(kù)函數(shù))

    軟件測(cè)試-MySQL(六:數(shù)據(jù)庫(kù)函數(shù))

    這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • MySQL 在觸發(fā)器里中斷記錄的插入或更新?

    MySQL 在觸發(fā)器里中斷記錄的插入或更新?

    MySQL 不象其它有些數(shù)據(jù)庫(kù)可以在觸發(fā)器中拋出異常來(lái)中斷當(dāng)然觸發(fā)器的執(zhí)行以阻止相應(yīng)的SQL語(yǔ)句的執(zhí)行。在MySQL的目錄版本中還無(wú)法直接拋出異常。這樣我們?nèi)绾螌?shí)現(xiàn)呢?
    2009-07-07
  • mysql主從同步復(fù)制錯(cuò)誤解決一例

    mysql主從同步復(fù)制錯(cuò)誤解決一例

    Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids
    2011-05-05
  • centos7.4系統(tǒng)中yum源安裝mysql 5.6

    centos7.4系統(tǒng)中yum源安裝mysql 5.6

    本文給大家介紹的是如何在centos7.4系統(tǒng)中通過(guò)yum源安裝MySQL 5.6數(shù)據(jù)庫(kù),CentOS7默認(rèn)數(shù)據(jù)庫(kù)是mariadb, 但是 好多用的都是mysql ,但是CentOS7的yum源中默認(rèn)好像是沒(méi)有mysql的,今天我們就來(lái)看看具體如何操作
    2018-09-09
  • MySql在Mac上的安裝與配置詳解

    MySql在Mac上的安裝與配置詳解

    這篇文章主要介紹了MySql在Mac上的安裝配置,需要的朋友可以參考下
    2017-05-05
  • MySQL8.0無(wú)法啟動(dòng)3534的解決方法

    MySQL8.0無(wú)法啟動(dòng)3534的解決方法

    本文主要是記錄一下自己使用MySQL的一次踩坑經(jīng)歷,我的MySQL安裝好后,使用一周后的同一時(shí)間必定報(bào)連接失敗,然后查找發(fā)現(xiàn)是MySQL本地服務(wù)沒(méi)有啟動(dòng),下面就詳細(xì)的介紹一下
    2021-06-06

最新評(píng)論