SQL查询在同一个游戏中查找具有两个角色的所有艺术家

时间:2016-10-05 22:24:49

标签: sql oracle

我必须找到在同一部剧中扮演两个角色的艺术家;显示每个此类游戏的标题,艺术家的名字和两个角色 另外,我不能使用group by。 以下是表格:

Plays(Playid,Title,Director,Year,Cost)
Artists(Artistid,Name,Gender,Birthdate)
Roles(Playid,Artistid,Character)

这是我到目前为止所做的:

select title, a.name, p.playid, r.character, a.artistid 
   from plays p 
   left join roles r on p.playid = r.playid 
   left join artists a on r.artistid = a.artistid;

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

select p.title, a.name, r.character, r2.character
from roles r join roles r2 on (r.artistid = r2.artistid and 
                               r.playid = r2.playid and 
                               r.character < r2.character)
left join plays p on r.playid = p.playid
left join artists a on r.artistid = a.artistid

答案 1 :(得分:0)

添加以下内容:

其中a.Artistid in ( 选择Artistid 来自角色 Artistid小组 有计数(*)= 2 )

答案 2 :(得分:0)

请检查我的查询:

      Select a.Name,p.Title,r.Character from (select * from Artist where Artistid in
        (select Artistid from Role group by aid having count(rl.Character)>=2)) a 
        left join Role r on r.Artistid=a.Artistid
        left join Play p on r.Playid=p.Playid;