Mysql刪除重複資料、比較兩個表資料
阿新 • • 發佈:2019-01-24
#1.刪除t_discredit_company_info表重複資料(t_discredit_company_info表資料重複)
DELETE FROM t_discredit_company_info WHERE id IN ( SELECT * FROM ( SELECT id FROM t_discredit_company_info WHERE (company_id,attribute_year_month) IN ( SELECT company_id,attribute_year_month FROM t_discredit_company_info GROUP BY company_id,attribute_year_month HAVING COUNT(1) > 1 ) AND id NOT IN ( -- 查詢最小id的記錄 SELECT MIN(id) FROM t_discredit_company_info GROUP BY company_id,attribute_year_month HAVING COUNT(1) > 1 )) AS stu_repeat_copy );
#2.查詢t_discredit_company表比t_discredit_company_info表多了哪些資料(t_discredit_company表資料多,t_discredit_company_info表資料少)
SELECT count(*) FROM t_discredit_company o LEFT JOIN t_discredit_company_info w ON o.company_id=w.company_id
WHERE w.company_id IS NULL;
#3.刪除t_discredit_company表的多餘資料(t_discredit_company表資料多,t_discredit_company_info表資料少)
delete from t_discredit_company where t_discredit_company.company_id not in
(select company_id from t_discredit_company_info);