我對錶進行規範化相當陌生,而且我正在爲了從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
喔...我知道了。好吧,這真的很有幫助!謝謝 – Skyros