0
votes

SQL - Joindre intérieur (où, et, et non)

J'ai 2 tables: xxx

im essayant de sélectionner des noms d'étudiants qui ont suivi le cours "M" et je n'ai pas suivi le cours "H" ou "E '. < p> Ma solution est la suivante: xxx

aucune idée de ce qui ne va pas?


1 commentaires

Vous devriez faire de l'étudiant à la table des grades, comme clé étrangère


5 Réponses :


0
votes
SELECT * 
FROM STUDENTS S 
INNER JOIN GRADES G ON S.ID=G.STUDENTID and G.COURSEID = 'Math '
where s.id not in (
  select STUDENTID from GRADES where COURSEID = 'Eng ' OR G2.COURSEID = 'Heb'
)

0 commentaires

0
votes

Une autre approche à l'aide de la join gauche xxx

En outre, vous devez rester, StudentID à la table des grades comme clé étrangère


0 commentaires

0
votes

SELECT a.firstName, a.lastName, b.courseID
FROM STUDENTS a
INNER JOIN GRADES b
ON a.id = b.studentID
AND b.courseID = 'Math'
WHERE a.id NOT IN (
SELECT studentID FROM GRADES b 
WHERE courseID = 'Eng'
OR b.courseID = 'heb')


0 commentaires

0
votes

Il y a plusieurs façons de le faire.

Vous pouvez utiliser n'existe pas code> ou groupe par ... ayant code> comme suit: p>

-- 1

SELECT * FROM STUDENTS S
INNER JOIN GRADES G1 ON S.ID = G1.STUDENTID
WHERE G1.COURSEID = 'Math'
    AND NOT EXISTS (
        SELECT 1 FROM GRADES G2
        WHERE G2.STUDENTID = G1.STUDENTID
          AND G2.COURSEID IN ('Eng','Heb')
    );

-- 2 

SELECT S.ID, S.FIRSTNAME, S.LASTNAME FROM STUDENTS S
       INNER JOIN GRADES G1 ON S.ID = G1.STUDENTID
       WHERE COURSEID IN ('Math','Eng','Heb')
GROUP BY S.ID, S.FIRSTNAME, S.LASTNAME
HAVING SUM(CASE WHEN COURSEID IN ('Eng','Heb') THEN 1 END) = 0
   AND SUM(CASE WHEN COURSEID = 'Math' THEN 1 END) <> 0


0 commentaires

0
votes

Vous pouvez faire l'agrégation: xxx


0 commentaires