How to select all exams by its ID and title (from table 'exams'), that a student (in table 'grades') hasn't written yet?
TABLE: grades
+--------------+----+----
|student_number|e_id| ...
+--------------+----+----
|888075 |1 | ...
|888075 |2 | ...
|888075 |4 | ...
|637020 |2 | ...
+--------------+----+----
TABLE: exams
+----+------+
|e_id|title |
+----+------+
|1 |exam 1|
|2 |exam 2|
|3 |exam 3|
|4 |exam 4|
+----+------+
In this particular case I would expect the following output for student 888075
:
+--+------+
|id|title |
+--+------+
|3 |exam 3|
+--+------+
I just need the inverse selection of this:
SELECT e.e_id as id, e.title as title
FROM grades g
LEFT JOIN exams e
ON g.e_id = e.e_id
WHERE g.student_number = '888075'
Your query was close -- just reverse the joins and check for null
accordingly:
SELECT e.e_id as id, e.title as title
FROM exams e
LEFT JOIN grades g ON g.e_id = e.e_id
AND g.student_number = '888075'
WHERE g.e_id IS NULL
You need to select those e_id
that are not in e_id
's that student participated.
SELECT aa.e_id AS id, aa.title
FROM exams AS aa
WHERE e_id NOT IN (
SELECT e_id
FROM grades
WHERE student_number = '888075'
GROUP BY e_id
);
OR
SELECT aa.e_id AS id, aa.title
FROM exams AS aa
WHERE e_id NOT IN (
SELECT DISTINCT e_id
FROM grades
WHERE student_number = '888075'
);
OR
SELECT aa.e_id AS id, aa.title
FROM exams AS aa
WHERE NOT EXISTS (
SELECT e_id
FROM grades AS bb
WHERE aa.e_id = bb.e_id AND bb.student_number = '888075'
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.