我有3個表:問題類別,併爲problem_categories數查詢停止正常工作,我加入後where子句
我想獲得和計數與問題相關的所有類別的連接表,數每個問題有多少問題。
這裏是我的查詢不返回正確的結果:
SELECT
category_name ,
categories.category_id ,
problems.problem_id,
COUNT(problems.problem_id) as num_problems
FROM
problem_categories
left JOIN
categories
on
problem_categories.category_id = categories.category_id
left join
problems
on
problem_categories.problem_id = problems.problem_id
WHERE
problem_categories.problem_id = 266
GROUP BY
problems.problem_id , category_name
,但只要我拿出WHERE子句,它返回所有類別和問題的正確計數。但訣竅是,我需要查詢一個特定的問題_id
任何方式來調整這個查詢來得到它的權利?我在這裏做錯了什麼?
謝謝!
我想你需要使用USING()函數 – Chibuzo
@Chibuzo如何以及在哪裏? :)我以前沒有真正使用它。 – GeekedOut
嘗試將條件移入'having'子句(在'group by後面):移除'where',並在末尾添加'proble.problem_id = 266'。 – dasblinkenlight