mysql 删除表中重复记录只保留一条,delete from select 方式删除数据

        通常我们在向表中插入数据的时候,会排查这个记录是否已经存在,比如接收第三方的传输数据,根据流水号去查询这个记录是否已经存在,如果已经存在了这条数据,那么新插入的数据就会删除掉 ,也可以在插入前就查询这个流水号是否已经存在,这种在插入前需要循环判断,也可以在插入后查询重复的再去删除,这里我们测试的是批量插入后再去重

 一 ,批量插入后,查询有没有重复的流水号,  foreach 方式插入

insert into merc_trans_integral_info (
            bat_no, product_id, merc_id, merc_name, trans_amount, trans_fee_amount,
            trans_type, trans_card_type, trans_time, trans_ac_time, corg_sref_no,
            log_no, create_time, update_time, txn_bus_typ, txn_fee_flg
         ) values

        
            (
             #{batNo}, #{list.productType}, #{list.mercId}, #{list.mercName}, #{list.transAmt}, #{list.transFeeAmt},
             #{list.transType}, #{list.transCardType}, #{list.transTime}, #{list.transAcTime}, #{list.corgSrefNo},
             #{list.logNo}, #{createTime}, #{createTime}, #{list.txnBusTyp}, #{list.txnFeeFlg}
            )
        

插入后检查有没有查询的流水,有则删除, 根据流水分组,查询最小的ID,删除不是最小的 ID

DELETE
        FROM
        	merc_trans_integral_info
        WHERE
            trans_time= #{Date}
        and id NOT IN (
        		SELECT
        			dt.minNo
        		FROM
        			(
        				SELECT
        					MIN(id) AS minNo
        				FROM
        					merc_trans_integral_info
        				 where trans_time = #{Date}
        				GROUP BY
        					log_no
        			) dt
        	)

二 上面的方式会查询出当天所有的流水,再去删除,第二张方式,先获取到有重复流水的流水号和最小ID,然后删除 这种查询出来的数据量小,会排查没有重复的流水号

SELECT
         	log_no as logNo,
         	MIN(id) AS minId
         FROM
         	merc_trans_integral_info
         where trans_time = #{Date}
         GROUP BY
         	log_no
         HAVING
         	count(*) > 1
         order by log_no desc
DELETE
        FROM
        	merc_trans_integral_info
        WHERE  log_no =#{logNo}
        and id != #{minId}

三 结合上面两种方式,使用 delete from select 方式一次删除, 流水号在查询结果里面,删除的 ID 不在结果里面

DELETE
        FROM
        	merc_trans_integral_info
        WHERE
            trans_time = #{Date}

         and log_no IN (
        		SELECT
        			log_no
        		FROM
        			(
        				SELECT
        					log_no
        				FROM
        					merc_trans_integral_info
        				 where trans_time = #{Date}
        				GROUP BY
        					log_no
                HAVING
            	  count(*) > 1
        			) dt
        	)
         and id NOT IN (
        		SELECT
        			dt.minno
        		FROM
        			(
        				SELECT
        					MIN(id) AS minno
        				FROM
        					merc_trans_integral_info
        				 where trans_time = #{Date}
        				GROUP BY
        					log_no
                HAVING
            	  count(*) > 1
        			) dt
        	)

 

你可能感兴趣的:(mysql,mysql)