mysql删除重复记录并保留一条

DELETE
FROM
	goods_brand
WHERE
	brand_name IN ( select * from (
		SELECT
			brand_name
		FROM
			goods_brand
		GROUP BY
			brand_name
		HAVING
			COUNT(brand_name) > 1
	) as a)
AND id NOT IN ( select * from (
	SELECT
		MIN(id)
	FROM
		goods_brand
	GROUP BY
		brand_name
	HAVING
		COUNT(brand_name) > 1) as b
)
UPDATE chz_goods_img 
SET gi_status = 0 
WHERE
	gi_id NOT IN (
	SELECT
		* 
	FROM
		(
		SELECT
			gi1.gi_id 
		FROM
			chz_goods_img gi1
			LEFT JOIN chz_goods_img gi2 ON gi1.gi_g_id = gi2.gi_g_id 
			AND gi1.gi_status = 1 
			AND gi2.gi_status = 1 
			AND gi1.gi_type = gi2.gi_type 
			AND gi1.gi_id < gi2.gi_id 
		WHERE
			gi2.gi_id IS NULL 
			AND gi1.gi_status = 1 
		) AS t 
	);

更多的限制条件:

https://www.cnblogs.com/jiangxiaobo/p/6589541.html

你可能感兴趣的:(mysql)