0

我有这段代码,并且它们都单独工作,所以我认为它一定是错误放置的语法:

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';
4

1 回答 1

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';
于 2012-10-20T19:32:47.547 回答