MySQL 查詢並刪除多條重複記錄
阿新 • • 發佈:2019-02-07
重複記錄的出現,通常是用於程式後端驗證不嚴謹導致出現的垃圾資料,想要查詢並刪除,可通過以下實現方式
更多精彩
查詢重複記錄
查詢指定欄位相同且總數大於一的資料
- 先指明可能重複的欄位,可以是多個
- 再根據分組查詢結果,通過
HAVING
進行判斷
SELECT
msan.memberSurvey_id,
msan.question_id,
msan.option_id
FROM
pt_member_survey_answer msan
GROUP BY
msan.memberSurvey_id,
msan.question_id,
msan.option_id
HAVING
COUNT(*) > 1
根據上述查詢結果將重複資料全部查出
- 上述查詢會得到組合欄位中出現重複的情況
- 根據該情況即可查詢出涉及到的具體資料條數
SELECT
msa.*
FROM
pt_member_survey_answer msa
WHERE
(msa.memberSurvey_id, msa.question_id, msa.option_id) IN (
SELECT
msan.memberSurvey_id,
msan.question_id,
msan.option_id
FROM
pt_member_survey_answer msan
GROUP BY
msan.memberSurvey_id,
msan.question_id,
msan.option_id
HAVING
COUNT(*) > 1
)
ORDER BY
msa.memberSurvey_id ASC,
msa.question_id ASC,
msa.option_id ASC
刪除重複記錄
查詢涉及到重複情況的資料id
SELECT
msan.id AS 'id'
FROM
pt_member_survey_answer msan
GROUP BY
msan.memberSurvey_id,
msan. question_id,
msan.option_id
HAVING COUNT(*) > 1
刪除查詢到的資料id
- 注意此處對上述查詢結果直接獲取的話,會丟擲
You can't specify target table for update in FROM clause
異常 - 所以需要對查詢結果進行一次
SELECT
包裝,才可以 - 刪除查詢結果只能查詢出可能重複的情況,如果同一個情況存在兩條或更多的重複資料,一次刪除無法完全清空,可能需要執行多次刪除語句
DELETE FROM
pt_member_survey_answer
WHERE
id IN (
SELECT
rs.id
FROM (
SELECT
msan.id AS 'id'
FROM
pt_member_survey_answer msan
GROUP BY
msan.memberSurvey_id,
msan.question_id,
msan.option_id
HAVING COUNT(*) > 1
) rs
)