数据库多字段去重

# 查询

SELECT
	*
FROM
	shopee_finance_data_statistics_v2 a
WHERE
	(
		a.get_time,
		a.realname,
		a.payout_time
	) IN (
		SELECT
			get_time,
			realname,
			payout_time
		FROM
			shopee_finance_data_statistics_v2
		GROUP BY
			get_time,
			realname,
			payout_time
		HAVING
			count(*) > 1)

  

 

# 删除

DELETE
FROM
	shopee_finance_data_statistics_v2
WHERE
	id IN (
		SELECT
			id
		FROM
			(
				SELECT
					id
				FROM
					shopee_finance_data_statistics_v2 a
				WHERE
					(
						a.get_time,
						a.realname,
						a.payout_time
					) IN (
						SELECT
							get_time,
							realname,
							payout_time
						FROM
							shopee_finance_data_statistics_v2
						GROUP BY
							get_time,
							realname,
							payout_time
						HAVING
							count(*) > 1
					)
				AND id NOT IN (
					SELECT
						min(id)
					FROM
						shopee_finance_data_statistics_v2
					GROUP BY
						get_time,
						realname,
						payout_time
					HAVING
						count(*) > 1
				)
			) AS tmptb
	)

  

posted @ 2021-12-10 09:59  小王八+1  阅读(113)  评论(0编辑  收藏  举报