如何获得满足我的要求的陈述的值

如何获得满足我的要求的陈述的值

我尝试展示每对演员,两个演员都没有在任何人上演过
普通电影流派,而同时一个与另一流派一起播放过的流派至少为7

我这样做:

select a1.actor_id as i8opoios1,a2.actor_id as i8opoios2,((count(distinct(g1.genre_name))+count(distinct(g2.genre_name)))>=7) as result from actor as a1
inner join actor as a2 on a1.actor_id!=a2.actor_id

inner join role as r1 on a1.actor_id=r1.actor_id
inner join movie as m1 on m1.movie_id=r1.movie_id
inner join movie_has_genre as mg1 on mg1.movie_id=m1.movie_id
inner join genre as g1 on mg1.genre_id=g1.genre_id

inner join role as r2 on a2.actor_id=r2.actor_id
inner join movie as m2 on m2.movie_id=r2.movie_id
inner join movie_has_genre as mg2 on mg2.movie_id=m2.movie_id
inner join genre as g2 on mg2.genre_id=g2.genre_id

where a1.actor_id<a2.actor_id and mg1.genre_id!=mg2.genre_id
group by a1.actor_id,a2.actor_id;


该查询返回所有未曾在任何人处表演的演员对
常见的电影类型,如果组合为1(真),则它们使用7种或更多类型,如果没有,则为0(FALSE)。

Tables and their columns:

actor(actor_id,name)
role(actor_id,movie_id)
movie(movie_id,title)
movie_has_genre(movie_id,genre_id)
genre(genre_id,gender_name)

最佳答案

将条件添加到where子句以限制行。

SELECT
    a1.actor_id as i8opoios1,
    a2.actor_id as i8opoios2,
    IF((count(distinct(g1.genre_name))+count(distinct(g2.genre_name)))>=7,1,0) as result
FROM actor as a1
INNER JOIN actor as a2
    on a1.actor_id != a2.actor_id
INNER JOIN role as r1
    on a1.actor_id = r1.actor_id
INNER JOIN movie as m1
    on m1.movie_id = r1.movie_id
INNER JOIN movie_has_genre as mg1
    on mg1.movie_id = m1.movie_id
INNER JOIN genre as g1
    on mg1.genre_id = g1.genre_id
INNER JOIN role as r2
    on a2.actor_id = r2.actor_id
INNER JOIN movie as m2
    on m2.movie_id = r2.movie_id
INNER JOIN movie_has_genre as mg2
    on mg2.movie_id = m2.movie_id
INNER JOIN genre as g2
    on mg2.genre_id = g2.genre_id
WHERE a1.actor_id < a2.actor_id
    AND mg1.genre_id != mg2.genre_id
HAVING IF((count(distinct(g1.genre_name))+count(distinct(g2.genre_name)))>=7,1,0) = 1
GROUP BY a1.actor_id,a2.actor_id;

关于mysql - 如何获得满足我的要求的陈述的值(value)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50570400/

10-11 02:50