我无法根据两个条件获取所需的数据,并且我有此数据库:
library(dplyr)
before <- after <- 4
df %>%
mutate(a = "%%0%d.%df" %>%
sprintf(before + after + 1, after) %>%
sprintf(a) %>%
chartr(".", ",", .))
我想接待所有出现在Hanoh levin Play中但没有出现在Nisim Aloni Play中的演员(在本案中,除了shir以外,请把所有演员都收起。
我尝试这样做:
a
1 0002,5000
2 0003,5600
3 0004,5000
我得到一个错误,或者如果我稍做改动就什么也没得到。 根据数据库,我不应该接受女演员Shir,因为她出演了两位导演的戏剧。 我做错了什么?
谢谢
答案 0 :(得分:3)
您可以对联接使用以下查询:
select a_name
from Actor a
join Role r on r.a_id = a.a_id
join Play p on p.p_id = r.p_id
where exists ( select 1 from Play where director = 'Hanoh levin' and p_id = p.p_id )
and not exists ( select 1 from Play where director = 'Nisim Aloni' and p_id = p.p_id );
A_NAME
------
Shir
Noa
Osher
答案 1 :(得分:2)
加入3个表并按演员名称分组:
select a.a_name
from actor a inner join role r
on r.a_id = a.a_id
inner join play p
on p.p_id = r.p_id
where p.director in ('Hanoh levin', 'Nisim Aloni')
group by a.a_name
having min(p.director) = 'Hanoh levin' and max(p.director) = 'Hanoh levin'
请参见demo
结果:
| a_name |
| ------ |
| Noa |
| Osher |
答案 2 :(得分:1)
只需与主管联系即可。
select a_name from Actor a
join Role r on r.a_id = a.a_id
join Play p on p.p_id = r.p_id
where p.director = 'Hanoh levin'
and p.director <> 'Nisim Aloni'