我必须为我的学校创建一个数据库,现在我有这三个表:
Note Examination Subject
+------+------+------+--------+ +------+----+--------+ +------+----------+
| _idS | _idP | Note | Punkte | | _idP | idF| Thema | | _idF | Name |
+------+------+------+--------+ +------+----+--------+ +------+----------+
| 1 | 2 | 4 | 55 | | 1 | 2 | Test 1 | | 1 | Englisch |
| 2 | 2 | 2 | 80 | | 2 | 4 | Test 2 | | 2 | Deutsch |
| 3 | 2 | 1 | 95 | | 3 | 4 | Test 3 | | 3 | Mathe |
| 1 | 3 | 1 | 90 | +------+----+--------+ | 4 | Physik |
+------+------+------+--------+ +------+----------+
现在我想Notes
从一个由他给出的特殊科目Name
和一个由他给出的特殊学生中选择所有内容ID (_idS)
。我尝试过这样的事情:
SELECT N._idP, N.Note, N.Punkte, E.Thema FROM Note N, Examination E
WHERE N._idS='1' AND N._idP=(
SELECT E._idP FROM Examination E WHERE E.idF=(
SELECT S._idF FROM Subject S WHERE S.Name='Physik')
);
// N._idS=1 is the special pupil
// S.Name='Physik' is the special subject
但这不会导致正确的输出。为了让它更清楚一点:我想要这个输出:
+--------+--------+----------+---------+
| N._idP | N.Note | N.Punkte | E.Thema |
+--------+--------+----------+---------+
| 2 | 4 | 55 | Test 2 |
| 3 | 1 | 90 | Test 3 |
+--------+--------+----------+---------+
我的 SQL 语句有什么问题,我必须更改什么才能获得上述结果?所有开头带下划线的列名_
都是PRIMARY KEYS
. 其他都是FOREIGN KEYS
。