亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

mysql 去重留一

發布時間:2020-07-02 02:03:46 來源:網絡 閱讀:1525 作者:zxxiong 欄目:MySQL數據庫

首先先分析一下 我們現在的目的 是 查詢到這倆張表的所有數據 然后進行刪除重復記錄 每條數據只保留一條

第一步:
查詢以下倆張表的重復記錄 (關鍵字段重復>1)
ks_examcity 、 ks_examdistrict

select * from ks_examcity group by examSubjectID,city,province having count(examSubjectID)>1;
select * from ks_examdistrict group by examSubjectID,district,city having count(examSubjectID)>1;
第二步:
   查詢這兩張表中 每條記錄的第一條記錄 (每條記錄重復中的第一條 id最小)
select min(id)
  from ks_examcity
 group by examSubjectID, city, province
having count(examSubjectID)> 1

SELECT min(id)
  FROM `ks_examdistrict`
 GROUP BY `examSubjectID`, `district`, `city`
HAVING COUNT(`examSubjectID`)> 1
第三步:
    聯查: 查詢所有的重復數據以及重復記錄中第一條以外的數據
select `examSubjectID`
  from ks_examcity
 group by examSubjectID, city, province
having count(examSubjectID)> 1 )
   and id not in(
select min(id)
  from ks_examcity
 group by examSubjectID, city, province
having count(examSubjectID)> 1

SELECT `examSubjectID`
  from `ks_examdistrict`
 group by `examSubjectID`, `district`, `city`
HAVING COUNT(`examSubjectID`)> 1)
   and id not in(
SELECT min(id)
  FROM `ks_examdistrict`
 GROUP BY `examSubjectID`, `district`, `city`
HAVING COUNT(`examSubjectID`)> 1
第四步:  查詢以上查詢到的數據的所有id  并以查詢到的id作為條件 進行刪除

delete from `ks_examcity`  where id IN(  select id from (
select  id
  from ks_examcity
 where `examSubjectID` in(
select `examSubjectID`
  from ks_examcity
 group by examSubjectID, city, province
having count(examSubjectID)> 1 )
   and id not in(
select min(id)
  from ks_examcity
 group by examSubjectID, city, province
having count(examSubjectID)> 1)) as  tmpresult)

======================================================

DELETE
  FROM `ks_examdistrict`
 where id IN(
SELECT id
  from(
select id
  from `ks_examdistrict`
 where `examSubjectID` in(
SELECT `examSubjectID`
  from `ks_examdistrict`
 group by `examSubjectID`, `district`, `city`
HAVING COUNT(`examSubjectID`)> 1)
   and id not in(
SELECT min(id)
  FROM `ks_examdistrict`
 GROUP BY `examSubjectID`, `district`, `city`
HAVING COUNT(`examSubjectID`)> 1)) as tmpresult)

參考文章 :https://www.cnblogs.com/jdbeyond/p/8157224.html

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

桐乡市| 东光县| 郸城县| 东乡族自治县| 息烽县| 九龙县| 南昌县| 齐齐哈尔市| 蚌埠市| 交口县| 犍为县| 襄垣县| 应城市| 类乌齐县| 伊川县| 广元市| 宁夏| 醴陵市| 九龙坡区| 高尔夫| 元朗区| 仙居县| 汝城县| 隆回县| 益阳市| 嘉荫县| 凤阳县| 伊通| 福贡县| 渝中区| 巴青县| 济源市| 莆田市| 沂源县| 阿坝县| 巴彦县| 沙湾县| 广昌县| 伊吾县| 乐安县| 蒙自县|