查询所有观看过相同电影的人

时间:2015-03-09 03:29:05

标签: mysql sql database

假设我看了不止一部电影,我如何找到所有看过我看过的所有电影的人呢?

     MovID  Name   Movie name
    ---------------------
    1     | me     |Batman
    2     | me     |Spider Man
    1     | Jason  |Batman
    2     | Jason  |Spider Man
    3     | Jason  |King kong 
    3     | Jim    |King kong
    4     | Jim    |Titanic

结果应该给我杰森。谢谢!

1 个答案:

答案 0 :(得分:1)

这是一种方式:

select name
  from tbl
 where movid in (select movid from tbl where name = 'me')
   and name <> 'me'
 group by name
having count(*) = (select count(*) from tbl where name = 'me')

小提琴: http://sqlfiddle.com/#!9/3120e/2/0