私はこのコードを持っていますが、個別にそれらはすべて機能するので、間違って配置された構文であるに違いないと思います:
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';
上記の International は、Local、Regional、National、または 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';