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);

 

原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/244797.html

(0)
上一篇 2022年4月17日
下一篇 2022年4月17日

相关推荐

发表回复

登录后才能评论