【发布时间】:2012-02-01 06:21:57
【问题描述】:
这是我的查询:
SELECT usr.id,
count(DISTINCT sol.id) as 'Asked',
count(DISTINCT ans.id) as 'Answered',
sum(DISTINCT CASE ans.accepted WHEN 1 THEN 1 ELSE 0 end) as 'Accepted'
FROM tbl_users usr
LEFT JOIN tbl_solutions sol on sol.authorID = usr.id
LEFT JOIN tbl_solution_answers ans on ans.authorID = usr.id
group by usr.id, sol.authorID
我上面对sum(DISTINCT CASE ans.accepted WHEN 1 THEN 1 ELSE 0 end) 的查询只返回 1,尽管我知道情况并非如此。我尝试在ans.authorID 上添加一个组子句,但没有效果。
如何从tbl_solution_answers ans 表中获取所有行的总和,其中authorID 是tbl_users.id 和Accepted 是1。
【问题讨论】: