微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐