我有这个代码,并且它们都是单独的,所以我认为它必须是错误放置的语法:
SELECT i.*, o.organ_name, o.organ_logo, vtable.*, cc.ccount
FROM heroku_056eb661631f253.op_ideas i
JOIN
以上收集来自op_ideas表的所有记录。
(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
然后上面搜索另一个表并计算每条记录的投票数并将其添加到该行中。
JOIN
(SELECT ccc.idea_id AS cid, COUNT(ccc.idea_id = 1 or null) AS ccount
FROM op_comments ccc
GROUP BY idea_id
) AS cc ON cid = i.idea_id
以上计算了idea_id附加了多少条评论,并将其添加到主行。
LEFT JOIN op_organs o ON i.post_type = o.organs_id
以上将另一个表连接到现有行,该行可能是空白也可能不是空白
WHERE idea_geo = 'International';
以上国际被替换为可能相等的变量:本地,区域,国家或国际。
问题:查询触发但返回为空,但如果单独放置,它们会起作用。有人可以指出我正确的方向。
以下是帮助阅读的完整代码:
Here is another issue, I think I have placed the code in the wrong place. Wanting to add another sub SELECT to count how many comments are per idea:
SELECT i.*, o.organ_name, o.organ_logo, vtable.*, cc.ccount
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
JOIN
(SELECT ccc.idea_id AS cid, COUNT(ccc.idea_id = 1 or null) AS ccount
FROM op_comments ccc
GROUP BY idea_id
) AS cc ON cid = i.idea_id
LEFT JOIN op_organs o ON i.post_type = o.organs_id
WHERE idea_geo = 'International';
提前致谢。
编辑新解决方案
感谢WayneC和Conrad,我们有一个完全有效的查询。
以下是代码:
SELECT i.*, o.organ_name, o.organ_logo, vtable.*
FROM heroku_056eb661631f253.op_ideas i
LEFT JOIN
(SELECT v.idea_Id, cc.*,
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
LEFT JOIN
(SELECT idea_id AS id,COUNT(*) AS ccount
FROM op_comments cco
GROUP BY cco.idea_id
) AS cc ON cc.id = v.idea_id
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';
答案 0 :(得分:0)
答案如下:
SELECT i.*, o.organ_name, o.organ_logo, vtable.*
FROM heroku_056eb661631f253.op_ideas i
LEFT JOIN
(SELECT v.idea_Id, cc.*,
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
LEFT JOIN
(SELECT idea_id AS id,COUNT(*) AS ccount
FROM op_comments cco
GROUP BY cco.idea_id
) AS cc ON cc.id = v.idea_id
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';