你可以嘗試這樣的查詢:
SELECT
tblProjects.name AS project,
tblCategories.name AS category
FROM
tblProjects
INNER JOIN tblPrCat USING (t1_id)
INNER JOIN tblCategories USING (t3_id)
WHERE
/* Your search criteria here */
注意:你會看到每個項目相關的所有類別名稱,但如果有一個以上的類別每個項目,你」會看到什麼似乎是重複的項目名稱。準備在代碼中處理這個問題。
編輯: 我假設(可能是錯誤的),每個項目至少有一個類別。爲了確保你也看到沒有項目名稱,而是使用此:
SELECT
tblProjects.name AS project,
IFNULL(tblCategories.name, 'No categories!') AS category
FROM
tblProjects
LEFT JOIN tblPrCat USING (t1_id)
LEFT JOIN tblCategories USING (t3_id)
WHERE
/* Your search criteria here */
編輯: 顯示每個項目只有一次,但顯示的類別名稱的總結作爲一個逗號分隔的列表,使用此:
SELECT
tblProjects.name AS project,
IFNULL(GROUP_CONCAT(tblCategories.name), 'No categories!') AS category
FROM
tblProjects
LEFT JOIN tblPrCat USING (t1_id)
LEFT JOIN tblCategories USING (t3_id)
WHERE
/* Your search criteria here */
GROUP BY
tblProjects.t1_id
謝謝你,有人包括使用替代山坳=關口 – WebChemist