计算在另一个角色中具有相同值的角色

时间:2012-10-23 15:58:18

标签: mysql database

如何为制作人,作家,作家选择具有相同角色值的标题数?

看起来像这样,

title | year | person | role(actor, director, producer or writer)

推理应该沿着这条线,

SELECT COUNT(title) 
FROM movie_table 
WHERE role(writer,director, producer)= same person

2 个答案:

答案 0 :(得分:0)

尝试

SELECT COUNT(title) 
from movie_table
WHERE role in ('producer', 'writer')
and movie_person is not null
group by title
having count(distinct role) = 2
and count(distinct movie_person) = 1

答案 1 :(得分:0)

使用具有可选连接条件的自连接(不是同一行):

SELECT COUNT(t1.title)
FROM movie_table t1
JOIN movie_table t2 ON t1.id != t2.id 
    AND (t1.writer = t2.writer
      OR t1.director = t2.director
      OR t1.producer = t2.producer)