2
votes

SQL - Utilisation de deux conditions ou plus dans select avec AND

Je ne peux pas obtenir les données que je veux selon deux conditions et j'ai cette BASE DE DONNÉES:

  SELECT a_id, a_name FROM (
     Select * FROM Actor JOIN Play, Role ON Actor.a_id=Role.a_id AND 
     Role.p_id=Play.p_id WHERE  
        director='Hanoh levin') AND NOT EXISTS (
          Select * FROM Actor JOIN Play, Role ON Actor.a_id=Role.a_id AND 
             Role.p_id=Play.p_id WHERE  
                director='Nisim Aloni'); 

Je souhaite recevoir tous les acteurs qui sont apparus dans Hanoh levin Play et n'apparaissaient pas dans Nisim Aloni Play (Dans le cas présent, récupérez-les tous sauf de shir.

J'essaye de faire ceci:

Theatre(t_name, city, sinceYear);
Actor(a_id, a_name, birthYear, city);
Play(p_id, title, playwright, director, year);
Role(a_id, p_id, role, t_name);

Theatre:
Abima|tel aviv|1/1/1960
Miskan|beer sheba|20/2/1970
Akamri|haifa|15/3/1989

Actor:
204458952|Dani|22/1/1993|sderot
221354875|Osher|23/8/1995|beer sheba
251445841|Avi|5/12/1998|haifa
304226152|Noa|12/5/1992|tel avia
304804123|Shir|10/6/1990|beer sheba

Play:
111|Meri lo|Lior|Hanoh levin|2010
222|Tov agmad|Asaf|Nisim Aloni|2016
333|Songs|Ami|Hanoh levin|2018
444|Run it|Sami|Nisim Aloni|2010

Role:
304804123|111|meri|Abima
304226152|111|sonia|Abima
251445841|222|tov tov|Miskan
204458952|222|gamad |Abima
221354875|333|prince|Akamri
304804123|222|princes|Abima

J'obtiens une erreur ou je ne le fais pas obtenir quelque chose si je le change un peu. D'après la base de données, je ne devrais pas accepter l'actrice Shir car elle apparaît dans les pièces de théâtre de deux réalisateurs. qu'est-ce que je fais de mal?

merci

sql

0 commentaires

3 Réponses :


2
votes

Vous pouvez utiliser la requête suivante avec les jointures:

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

Démo


0 commentaires

1
votes

Rejoignez 3 tables et regroupez-les par nom d'acteur:

| a_name |
| ------ |
| Noa    |
| Osher  |

Voir le démo
Résultats:

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'


0 commentaires

1
votes

Vérifiez simplement le réalisateur.

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'


0 commentaires