查询记录,其中的数量等于另一个表中的数量

时间:2014-10-08 13:27:00

标签: mysql sql select group-by

例如,movieCopy(cp_id, movie_id), rental(cp_id, Date)电影有一些副本,副本具有唯一ID。租赁表代表租用电影拷贝的记录(不要只关心租一次)。我想查询哪个电影已经全部出租,这意味着电影的副本数量等于租赁中可能的cp_id数量。 MYSQL数据库

movie `cp_id   movie_id               rental cp_id   Date
       101        1                           101    2001
       102        1                           102    2001
       103        1                           103    2001
       104        2                           104    2002
       105        2                           106    2002
       106        3 ` 

我想选择movie_id,1和3

1 个答案:

答案 0 :(得分:0)

根据您提供的列,这应该有效:

Select movie_id From movie Where movie_id Not Exists (Select Movie_id From movie Where cp_id Not Exists (Select distinct cp_id From rental Where date between date1 and date2)

date1和date2只是占位符,以防你想过滤它们。