您可以尝试这样的查询:
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