问题
I'm having some problems with INTERSECT command
. Hope someone could help me.
I want to get the the movieid that appears in the first and second SELECT
. After that I want to use these data (that could be in a LIMIT
of 10) to receive the titles of the movie in another table.
Something like this, but I'm not doing right:
SELECT movieid
FROM ratings
WHERE votes > 0
INTERSECT SELECT movieid FROM genres WHERE genre = '$_SESSION[genero]'
In this case I should get the movied that appear both on ratings and genres tables.
After this, I want to get these movieids and search the table movies for the movieid and finally show the title. Thank you!
回答1:
As far as I understood the question, I think this is what you are looking for
select title , movieid from movies
where movieid in
(
SELECT movieid FROM ratings
WHERE votes > 0
INTERSECT
SELECT movieid FROM genres
WHERE genre = '$_SESSION[genero]'
)
回答2:
select distinct m.title , movieid
from
movies m
inner join
ratings r using (movieid)
inner join
genres g using (movieid)
where r.votes > 0 and g.genre = '$_SESSION[genero]'
来源:https://stackoverflow.com/questions/33294345/how-to-intersect-datas-from-tables-on-postgres