数据库请求

时间:2017-06-12 06:47:50

标签: sql database request

我想对我的数据库执行以下请求:

我正在寻找那些在最少10部电影中与另一个人一起担任演员(工作)的人。你有任何提示或建议,我怎么能解决这个问题?

  • 生产(生产,标题)
  • MOVIE(生产 - >生产,类型)
  • PERSON(person,name,realname,birth_country)作品(person - >
  • PERSON,制作 - >生产,工作)

2 个答案:

答案 0 :(得分:0)

这似乎是一种更适合像Neo4j这样的图形数据库的问题,但这里是一个关系SQL解决方案:

select w1.person as person1
       , w2.person as person2
       , count(w1.production) as joint_productions
 from works w1
     join works w2
          on w1.production = w2.production
 where w1.person != w2.person
 group by w1.person, w2.person having count(w1.production) >= 10
 order by w1.person, w2.person;

这显示了演员和他们在一起的电影数量。它包括倒数,即

 bill murray, dan ackroyd, 10
 dan ackroyd, bill murray, 10

如果你只想要一组名字,你可以这样做:

 select distinct w1.person 
 from works w1
     join works w2
          on w1.production = w2.production
 where w1.person != w2.person
 group by w1.person, w2.person having count(w1.production) >= 10
 order by w1.person;

答案 1 :(得分:0)

我可能找到了没有子请求的解决方案。这是对的吗?

select w1.person as person1, w2.person as person2, p1.name , p2.name, count(w1.production) as joint_productions
from works w1 join works w2 on w1.production = w2.production
join person p1 on w1.person = p1.person
join person p2 on w2.person = p2.person
where w1.person != w2.person
group by w1.person, p1.name, p2.name, w2.person having count(w1.production)
>= 10 order by w1.person, w2.person