SELECT movies.movie_name, movies.year FROM movies
INNER JOIN (SELECT movie_name, year FROM movies
GROUP BY movie_name,year HAVING count(movie_id) > 1) dup ON movies.movie_name = dup.movie_name
and movies.year = dup.year
似乎是一个合理的开始......
通过删除一个我假设你的意思是保留一个,不要忘记你可以有多个重复
假设我们将保留第一个摆脱其余的,并且具有最早 movie_id 的那个是第一个
所以
Select Min(Movie_id), Movie_Name, Year From Movies Group By Movie_Name, Year
会给你所有可以保留的
Select Movie_id,Movie_Name,Year From Movies m
Left Join
(Select Min(Movie_id), Movie_Name, Year From Movies Group By Movie_Name, Year) keep
On keep.movieid = m.movieid Where keep.Movie_Id is null
以上是电影中的所有记录,但不在我们想要保留的所有查询中。
所以这给了我们所有你想要摆脱的东西。看在克苏鲁的份上,不要相信我或你自己!删除前先备份!
Delete m From Movies m
Left Join
(Select Min(Movie_id), Movie_Name, Year From Movies Group By Movie_Name, Year) keep
On keep.movieid = m.movieid Where keep.Movie_Id is null
So now we've took the query we proved (you did prove it didn't you! ) and instead of selecting the offending records we are deleting them.
Don't forget the back up!