zoukankan      html  css  js  c++  java
  • MySQL 删除重复数据实例

    如何删除重复数据

    业务场景:删除评论表中对同一订单同一商品的重复评论,只保留最早的一条。

    1. 查看是否存在对于同一订单同一商品的重复评论。
    SELECT order_id,product_id,COUNT(*) FROM product_comment GROUP BY order_id,product_id HAVING COUNT(*)>1;
    1. 备份product_comment表。
    CREATE TABLE bak_product_comment_18051801 LIKE product_comment;
    
    INSERT INTO bak_product_comment_18051801 SELECT * FROM product_comment;
    1. 删除同一订单的重复评论。
    DELETE a
    FROM product_comment a
    JOIN(
        SELECT order_id,product_id,MIN(comment_id) AS comment_id
        FROM product_comment
        GROUP BY order_id,product_id
        HAVING COUNT(*)>=2
    ) b ON a.order_id=b.order_id AND a.product_id=b.product_id
    AND a.comment_id>b.comment_id
    
  • 相关阅读:
    HDU
    HDU
    HDU
    HDU
    西电网络赛
    西电网络赛
    西电网络赛
    西电网络赛
    西电网络赛
    西电网络赛
  • 原文地址:https://www.cnblogs.com/yizhiamumu/p/9055119.html
Copyright © 2011-2022 走看看