溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

數(shù)據(jù)庫刪除重復(fù)數(shù)據(jù)

發(fā)布時間:2020-06-25 18:31:04 來源:網(wǎng)絡(luò) 閱讀:933 作者:bs開發(fā)小白 欄目:MySQL數(shù)據(jù)庫
               當(dāng)數(shù)據(jù)庫數(shù)據(jù)量大的時候,刪除重復(fù)元素是很討厭的,有時候怎么也寫不對,閑下來整理一下刪除
         重復(fù)數(shù)據(jù)的sql,數(shù)據(jù)表是之前看mysql教學(xué)視頻的素材,但是找不到哪里的了,如果原作者看到請
         留言。

    -- 創(chuàng)建數(shù)據(jù)表

    CREATE TABLE IF NOT EXISTS tdb_goods(
        goods_id    SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
        goods_name  VARCHAR(150) NOT NULL,
        goods_cate  VARCHAR(40)  NOT NULL,
        brand_name  VARCHAR(40)  NOT NULL,
        goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0,
        is_show     BOOLEAN NOT NULL DEFAULT 1,
        is_saleoff  BOOLEAN NOT NULL DEFAULT 0
    );

 -- 寫入記錄

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('R510VC 15.6英寸筆記本','筆記本','華碩','3399',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('Y400N 14.0英寸筆記本電腦','筆記本','聯(lián)想','4899',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('G150TH 15.6英寸游戲本','游戲本','雷神','8499',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('X550CC 15.6英寸筆記本','筆記本','華碩','2799',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('X240(20ALA0EYCD) 12.5英寸超極本','超級本','聯(lián)想','4999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('U330P 13.3英寸超極本','超級本','聯(lián)想','4299',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('SVP13226SCB 13.3英寸觸控超極本','超級本','索尼','7999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('iPad mini MD531CH/A 7.9英寸平板電腦','平板電腦','蘋果','1998',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('iPad Air MD788CH/A 9.7英寸平板電腦 (16G WiFi版)','平板電腦','蘋果','3388',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES(' iPad mini ME279CH/A 配備 Retina 顯示屏 7.9英寸平板電腦 (16G WiFi版)','平板電腦',
 '蘋果','2788',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('IdeaCentre C340 20英寸一體電腦 ','臺式機','聯(lián)想','3499',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('Vostro 3800-R1206 臺式電腦','臺式機','戴爾','2899',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('iMac ME086CH/A 21.5英寸一體電腦','臺式機','蘋果','9188',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('AT7-7414LP 臺式電腦 (i5-3450四核 4G 500G 2G獨顯 DVD 鍵鼠 Linux )','臺式機',
 '宏碁','3699',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('Z220SFF F4F06PA工作站','服務(wù)器/工作站','惠普','4288',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('PowerEdge ×××10 II服務(wù)器','服務(wù)器/工作站','戴爾','5388',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('Mac Pro MD878CH/A 專業(yè)級臺式電腦','服務(wù)器/工作站','蘋果','28888',
 DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES(' HMZ-T3W 頭戴顯示設(shè)備','筆記本配件','索尼','6999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('商務(wù)雙肩背包','筆記本配件','索尼','99',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('X3250 M4機架式服務(wù)器 2583i14','服務(wù)器/工作站','IBM','6888',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('玄龍精英版 筆記本散熱器','筆記本配件','九州風(fēng)神','',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) 
 VALUES(' HMZ-T3W 頭戴顯示設(shè)備','筆記本配件','索尼','6999',DEFAULT,DEFAULT);

 INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff)
 VALUES('商務(wù)雙肩背包','筆記本配件','索尼','99',DEFAULT,DEFAULT);

 --插入重復(fù)數(shù)據(jù)
 INSERT tdb_goods(goods_name,cate_id,brand_id) 
 SELECT goods_name,cate_id,brand_id,goods_price FROM tdb_goods
 WHERE goods_id IN (19,20)

 --查詢重復(fù)數(shù)據(jù)
 SELECT goods_id,goods_name,goods_price FROM tdb_goods 
 GROUP BY goods_name,goods_price HAVING count(goods_name) >= 2

 --將要刪除的id查詢出來
 select t1.goods_id from  tdb_goods as t1 left  JOIN
 (SELECT goods_id,goods_name,goods_price FROM tdb_goods 
 GROUP BY goods_name,goods_price HAVING count(goods_name) >= 2) t2 
 on t1.goods_name = t2.goods_name where t1.goods_id>t2.goods_id

    --執(zhí)行刪除
delete t1 from  tdb_goods as t1 left  JOIN 
(SELECT goods_id,goods_name,goods_price FROM tdb_goods 
GROUP BY goods_name,goods_price HAVING count(goods_name) >= 2) t2 
on t1.goods_name = t2.goods_name 
where t1.goods_id>t2.goods_id    
向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI