How to INTERSECT datas from tables on postgres

独自空忆成欢 提交于 2019-12-13 07:27:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!