0

我有这个 sql 查询,它通过电影名称选择它在表中找到的重复项:

SQL:

SELECT movies.movie_name, movies.year FROM movies
INNER JOIN (SELECT movie_name FROM movies
GROUP BY movie_name HAVING count(movie_id) > 1) dup ON movies.movie_name = dup.movie_name
// want also to test for same year, not just movie_name i.e movies.year = dup.year

这可能吗?

4

2 回答 2

1
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!

于 2012-11-28T18:05:50.653 回答
0

I really like Tony's idea to use MIN. However, whole query can be simplier:

DELETE FROM movies WHERE movie_id NOT IN 
(SELECT MIN(movie_id) FROM movies GROUP BY movie_name, year);
于 2012-11-28T23:16:36.920 回答