mysql 去重,刪除重復數據保留id最大的數據


1 -- 查詢重復的數據
2 SELECT * FROM yy_visit_plan 
    WHERE
  (post_code,cust_tree_node_id,visit_year,visit_month,staff_type) IN
    (SELECT post_code,cust_tree_node_id,visit_year,visit_month,staff_type FROM yy_visit_plan
    GROUP BY post_code,cust_tree_node_id,visit_year,visit_month,staff_type HAVING COUNT(cust_tree_node_id) > 1); 3 4 -- 刪除重復數據 5 DELETE FROM yy_visit_plan
  WHERE
  (post_code,cust_tree_node_id,visit_year,visit_month,staff_type) IN
  (select * from
    (SELECT post_code,cust_tree_node_id,visit_year,visit_month,staff_type FROM yy_visit_plan
    GROUP BY post_code,cust_tree_node_id,visit_year,visit_month,staff_type HAVING COUNT(cust_tree_node_id) > 1)
   a)
  AND
  plan_id NOT IN
  (select * from
    (SELECT MAX(plan_id) as plan_id FROM yy_visit_plan GROUP BY post_code,cust_tree_node_id,visit_year,visit_month,staff_type HAVING COUNT(*)>1)
  b);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM