我正在匹配两个名为teacher_evaluation_question
和feedback
的表。
我想要来自teacher_evaluation_question
表的记录,其中student_id = 96,并且在反馈表中不存在
SELECT teacher_evaluation_question.*
FROM teacher_evaluation_question
LEFT JOIN feedback ON feedback.eq_id = teacher_evaluation_question.eq_id
WHERE feedback.student_id = 96 AND feedback.eq_id IS NULL
GROUP BY teacher_evaluation_question.eq_id;
这是表1(teacher_evaluation_question):
+-------+-----------------+------------+
| eq_id | question_set | createdate |
+-------+-----------------+------------+
| 3 | QT1/&/QT2/&/QT3 | 2019-04-03 |
| 4 | K1/&/K2/&/K3 | 2019-05-01 |
| 5 | A1/&/B1 | 2019-03-05 |
| 6 | C1/&/C2 | 2019-04-23 |
+-------+-----------------+------------+
这是表2(反馈):
+----+-------+------------+----------+--------+
| id | eq_id | student_id | question | rating |
+----+-------+------------+----------+--------+
| 13 | 26 | 2 | QT1 | 1 |
| 14 | 26 | 2 | QT2 | 4 |
| 15 | 26 | 2 | QT3 | 2 |
| 16 | 4 | 96 | K1 | 1 |
| 17 | 4 | 96 | K2 | 2 |
| 18 | 4 | 96 | K3 | 1 |
+----+-------+------------+----------+--------+
我期望这样的结果
+-------+-----------------+------------+
| eq_id | question_set | createdate |
+-------+-----------------+------------+
| 3 | QT1/&/QT2/&/QT3 | 2019-04-03 |
| 5 | A1/&/B1 | 2019-03-05 |
| 6 | C1/&/C2 | 2019-04-23 |
+-------+-----------------+------------+
注意:值student_id = 96是一个变量(它可能会根据获取请求参数$ x进行更改)
最佳答案
您可以执行以下操作:
SELECT teacher_evaluation_question.*
FROM teacher_evaluation_question
WHERE NOT EXISTS
(SELECT feedback.eq_id FROM feedback
WHERE feedback.eq_id = teacher_evaluation_question.eq_id)
GROUP BY teacher_evaluation_question.eq_id;