Mysql中使用SQL删除多字段相同的重复记录

项目中有一个实体项目类别与单项工程列别关联的表,比如实体项目类别1,包含单项工程类别2,3,4;

一段时间后发现添加了很多重复的关联记录,需要在数据库中删除;(为了避免再出现冗余的重复记录,添加时要增加验证是否是重复记录)

这里有几个点:

  • 通过group by以及having count(*)>1 来查询到重复记录;
  • 通过min(id)筛选出最小id保留到数据库中;
  • 通过临时表来存储查询到的重复记录,然后再删除;因为mysql不允许从查询结果中删除记录;

表:entity_project_types_project_contract_sub_types

判断重复的字段:entity_project_type_id, project_contract_sub_type_id

查询重复记录的sql:

SELECT
	*
FROM
	entity_project_types_project_contract_sub_types a
WHERE
	(
		a.entity_project_type_id,
		a.project_contract_sub_type_id
	) IN (
		SELECT
			entity_project_type_id,
			project_contract_sub_type_id
		FROM
			entity_project_types_project_contract_sub_types
		GROUP BY
			entity_project_type_id,
			project_contract_sub_type_id
		HAVING
			count(*) > 1
	)

查询结果:

Mysql中使用SQL删除多字段相同的重复记录_第1张图片

删除重复记录的SQL:

DELETE
FROM
	entity_project_types_project_contract_sub_types
WHERE
	id IN (
		SELECT
			id
		FROM
			(
				SELECT
					id
				FROM
					entity_project_types_project_contract_sub_types a
				WHERE
					(
						a.entity_project_type_id,
						a.project_contract_sub_type_id
					) IN (
						SELECT
							entity_project_type_id,
							project_contract_sub_type_id
						FROM
							entity_project_types_project_contract_sub_types
						GROUP BY
							entity_project_type_id,
							project_contract_sub_type_id
						HAVING
							count(*) > 1
					)
				AND id NOT IN (
					SELECT
						min(id)
					FROM
						entity_project_types_project_contract_sub_types
					GROUP BY
						entity_project_type_id,
						project_contract_sub_type_id
					HAVING
						count(*) > 1
				)
			) AS tmptb
	)

删除后再执行一遍查询重复记录的SQL,查询结果为0

你可能感兴趣的:(Code,ProblemResolve,FAQ)