我对规范化表格相当陌生,并且在从 3 个表格中获取正确信息时遇到了一些麻烦。我举了一个例子,涉及从不同图书馆预订书籍。我有 3 张桌子。书籍、地点、预订(如下所列):
//SQL query:
$sql =
"SELECT * FROM books
JOIN (location LEFT JOIN reservations ON location.locID = reservations.locID)
ON books.bookID = location.bookID
WHERE location.locID=2
";
如果我要列出校园 B 中的书籍,我希望达到的输出:
title |locName |status
Book 1|Campus B|1
Book 2|Campus B|0
Book 3|Campus B|0
Book 4|Campus B|0
Book 5|Campus B|1
出于某种原因,我绝对没有得到我认为我应该得到的输出,我很好奇是否有人有一些建议。我敢肯定,一旦我看到发生了什么,我就会明白我做错了什么。
table: books
bookID|title
1 | Book 1
2 | Book 2
3 | Book 3
4 | Book 4
5 | Book 5
table: location
locID|locName
1 | campus A
2 | campus B
3 | campus C
table: reservations
bookID|locID|status
1 | 1 | 1
3 | 1 | 1
4 | 1 | 1
1 | 2 | 1
5 | 2 | 1
4 | 3 | 1
5 | 3 | 1