我又发了一条这样的消息。但没有任何帮助。所以我做了更多的阅读,我有这段代码,它的工作点:
编辑新版本和新版本
SELECT i.*, o.organ_name, o.organ_logo, vtable.*
FROM heroku_056eb661631f253.op_ideas i
JOIN
(SELECT
COUNT(v.agree) as agree,
COUNT(v.disagree = 1 or null) as disagree,
COUNT(v.obstain = 1 or null) as abstain
FROM op_idea_vote v
) AS vtable
LEFT JOIN op_organs o ON i.post_type = o.organs_id
op_idea_投票表中只有1行,op_ideas表中只有3行。但它给了每一行在每一个操作想法行的总数
编辑工作解决方案
感谢WayneC,以下是工作代码:
SELECT i.*, o.organ_name, o.organ_logo, vtable.*
FROM heroku_056eb661631f253.op_ideas i
JOIN
(SELECT v.idea_Id,
COUNT(v.agree = 1 or null) as agree,
COUNT(v.disagree = 1 or null) as disagree,
COUNT(v.obstain = 1 or null) as abstain
FROM op_idea_vote v
GROUP BY v.idea_id
) AS vtable ON vtable.idea_id = i.idea_id
LEFT JOIN op_organs o ON i.post_type = o.organs_id
WHERE idea_geo = 'International';
最佳答案
试试这个
SELECT i.*, o.organ_name, o.organ_logo, vtable.*
FROM heroku_056eb661631f253.op_ideas i
JOIN
(SELECT
COUNT(v.agree) as agree,
COUNT(v.disagree = 1 or null) as disagree,
COUNT(v.obstain = 1 or null) as abstain
FROM op_idea_vote v
GROUP BY v.idea_id
) AS vtable ON vtable.idea_id = i.idea_id
LEFT JOIN op_organs o ON i.post_type = o.organs_id
下面的答案缺少分组依据和vtable的连接条件
关于mysql - 从第三张表看不到mySQL COUNT,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12874647/