2012-08-28 37 views
-2

我想从我的结果中排除某些东西。排除SQL与连接

我加入3个表,1,电影,1流派和1至2这些

合体,因此结果给出了一排每部电影为每个流派。

所以,如果我有两部电影,每种类型都有6个结果。

现在我希望能够说我不想要一部具有这种特殊风格的电影。如果我通过说AND tblmoviegenre.ID <> genre.ID来做到这一点,我只得到1行,但电影仍然在结果中,没有那一行的specefic流派。

有谁知道如何解决这个问题?

+2

请张贴一些代码... – Ankur

回答

3

我不确定你想要什么。如果我得到它,这应该做工精细

select distinct M.IdMovie, M.MovieName --And Other Fields 
from Movies M 
inner join MoviesPerGenre MPG on M.IdMovie = MPG.IdMovie 
inner join Genre G on MPG.IdGenre = G.IdGenre 
where M.IdMovie not in (Select IdMovie from MoviesPerGenre where IdGenre = 58) 

与Id的流派取代58要筛选

0

你想排除电影,如果它有这种流派,即使它也是在其他流派不被排除?

select <whatever> 
from movie, genre, moviegenre 
where <join conditions> 
and not exists (select 'x' from moviegenre where movie.id = moviegenre.id and moviegenre = "some genere you want to exclude) 
0

比方说,你的第三个表是

| movie | genre 
---------- ------------- 
     1 | 2 
     2 | 2 
     3 | 3 
     3 | 2 
     4 | 1 

,现在你想排除所有类型2,那么你可以写

SELECT movie.name,genre.name 
FROM movie inner join combine_info on movie.id = combine_info.movie, 
     genre inner join combine_info on genre.id = combine_info.movie 
WHERE movie.id NOT IN(SELECT movie 
          FROM combine_info 
          WHERE genre = 2); 
+0

对老j说不, oin语法! –

+0

@ElVieejo为什么会这样,是否会导致性能问题? – Ankur

+0

很难阅读,内部连接更具有表达性 –