我在構建正確的SQL JOIN
語句以從另一個表中選擇某些記錄時遇到問題。SQL JOIN:從其他表中選擇具有匹配ID的記錄
--Table Product:
ID
Name
CatID1
CatID2
和
--Table Category:
CatID
CategoryName
Product.CatID1
,Product.CatID2
都參考Category.CatID
所以,我真的要選擇Product
領域,並與Category.CategoryName
取代Product.CatID1
,Product.CatID2
(用於Product.CatID1
)和Category.CategoryName
(爲Product.CatID2
)。
這顯然是行不通的,但說明了什麼,我需要:
SELECT Product.ID, Product.Name,
Category.CategoryName as Product.CatID1,
Category.CategoryName as Product.CatID2
from product, categories;
根據Giorgos的回答,兩個左連接會更好,但是關於多對多連接的觀察很重要 – Pete
是的,有兩個CatID列通常是個不錯的主意。 – jarlh