此查询显示所有重复项:
SELECT movie_name, year
FROM movies
GROUP BY movie_name,year
HAVING count(movie_id) > 1
这仅显示每部电影(是否重复)的 movie_id,这些是要保留的行:
SELECT movie_name, year, min(movie_id) min_movie_id
FROM movies
GROUP BY movie_name,year
要删除重复项,您可以使用以下命令:
DELETE movies
FROM
movies inner join
(SELECT movie_name, year, min(movie_id) min_movie_id
FROM movies
GROUP BY movie_name,year) keep
on movies.movie_name = keep.movie_name
and movies.year=keep.year
and movies.movie_id<>keep.min_movie_id
或者你也可以使用这个查询:
delete from movies
where
(movie_name, year, movie_id) not in
(select movie_name, year, min_movie_id from
(SELECT movie_name, year, min(movie_id) min_movie_id
FROM movies
GROUP BY movie_name,year) keep)