2012-10-15 26 views
0

我有這樣的代碼,並單獨他們所有的工作,所以我想它一定是被放錯了位置的語法:兩個子查詢添加到除複雜的MySQL查詢返回任何記錄

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

哪個表是idea_geo嗎?如果它在op_organs中,那麼您需要將您的位置更改爲'idea_geo ='International'或idea_geo IS NULL' –

+0

@ConradFrix Hiya。它在表op_ideas i' –

+0

嗯你確定有兩個子查詢中的Idea_id的任何值 –

回答

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