Two subqueries added to addition complex mySQL query returning no records

a 夏天 提交于 2019-12-11 19:13:38

问题


I have this code, and individually they all work, so I think it must be the syntax which is incorrectly placed:

SELECT i.*, o.organ_name, o.organ_logo, vtable.*, cc.ccount
FROM heroku_056eb661631f253.op_ideas i
JOIN

The above collects all records from op_ideas table.

(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

The above then searches another table and counts the votes for each record and adds it to the row.

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

The above counts how many comments are attached to the idea_id and adds it to the main row.

LEFT JOIN op_organs o ON i.post_type = o.organs_id

The above joins another table to the existing row, which may or may not be blank

WHERE idea_geo = 'International';

International above is replaced with a variable which could equal: Local, Regional, National or International.

Issue: The query fires but comes back empty, but they work if put individually. Can someone please point me in the right direction.

Here is the full code to help read it:

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

Thanks in advance.

EDIT NEW SOLUTION

Thanks to WayneC and Conrad we have a fully working query.

Here is the code:

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

回答1:


And the answer is as follows:

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


来源:https://stackoverflow.com/questions/12900307/two-subqueries-added-to-addition-complex-mysql-query-returning-no-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!