mySQL查询计数,选择​​三个表

时间:2012-10-13 09:25:17

标签: php javascript mysql

希望我能帮助解决这个问题。我有三个表,一个有名为op_ideas的帖子(想法)。我有另一个名为op_idea_votes的表。这些可以通过idea_id链接。

当一个想法被发布时,民众可以对这个想法进行投票。然后使用最新的投票更新op_idea_votes。

然后我有另一个名为op_organs的表,根据发布的用户类型,它可能为空,也可能不为空。所以这个表由POST_TYPE(这是op_organs表的id)链接到op_organs.organ_id。

我需要选择所有这些表来创建一个包含要显示的所有信息的行,需要通过idea_id和post_type进行链接。但是我在op_idea_vote表上的3个列上也需要COUNT(同意,不同意,弃权)。

我已经厌倦了UNION,选择了SUM(CASE,但是我无法得到理想的效果。左边和内部连接但我似乎弄错了,部分或全部信息都丢失了。

应用程序是javascript和PHP,因此我试图减少对数据库和查询所在的PHP文件(使用ajax)的调用次数,这就是为什么我要尝试一次性。如果有更好的方式,我全都听见了。

这是从工具创建的当前尝试,但它返回空,但没有错误:

SELECT
op_ideas.author_id,
op_ideas.date,
op_ideas.deptid,
op_ideas.idea_geo,
op_ideas.idea_id,
op_ideas.idea_text,
op_ideas.idea_title,
op_ideas.modifed_date,
op_ideas.post_type,
op_ideas.user_country,
op_ideas.user_county,
op_ideas.user_loc,
op_organs.organ_name,
op_organs.organ_logo,
op_organs.active,
COUNT(op_idea_vote.agree),
COUNT(op_idea_vote.disagree),
COUNT(op_idea_vote.obstain)
FROM
heroku_056eb661631f253.op_ideas op_ideas,
heroku_056eb661631f253.op_organs op_organs,
heroku_056eb661631f253.op_idea_vote op_idea_vote
WHERE
op_ideas.idea_id = op_idea_vote.idea_id AND
op_ideas.post_type = op_organs.organs_id

所以数据看起来像这样:

+ ---------------------------------------------- -------------------------------------------------- -------------------------------- +
| idea_id | author_id | idea_title | idea_text |日期|同意|不同意|弃权| organ_name | organ_logo
+ ------------------------------------------------- -------------------------------------------------- ----------------------------- +
| 00001 | 10025225 | teste title |这里发短信| 2012 | 00005 | 0000004 | 000002 | Comp名称|图像/ .PNG
+ ------------------------------------------------- -------------------------------------------------- ----------------------------- +

不是一个很好的例子。但我试过了。

这是最新的代码。即使op_organs为null,它也会返回3行。但它没有显示投票数:

  SELECT i.*, o.organ_name, o.organ_logo
  FROM heroku_056eb661631f253.op_ideas i
  LEFT JOIN op_organs o ON i.post_type = o.organs_id
  JOIN 
    (SELECT
       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, op_ideas i
       GROUP BY v.idea_id
   ) as votes;

整天都花在这上面,读了这么多我的头在旋转。有人有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

SELECT
    op_ideas.author_id,
    op_ideas.date,
    op_ideas.deptid,
    op_ideas.idea_geo,
    op_ideas.idea_id,
    op_ideas.idea_text,
    op_ideas.idea_title,
    op_ideas.modifed_date,
    op_ideas.post_type,
    op_ideas.user_country,
    op_ideas.user_county,
    op_ideas.user_loc,
    op_organs.organ_name,
    op_organs.organ_logo,
    op_organs.active,
    COUNT(op_idea_vote.agree = 1 or null) as agrees,
    COUNT(op_idea_vote.disagree = 1 or null) as disagrees,
    COUNT(op_idea_vote.obstain = 1 or null) as absteins
FROM
    heroku_056eb661631f253.op_ideas op_ideas,
    left join
    heroku_056eb661631f253.op_organs op_organs on op_ideas.post_type = op_organs.organs_id,
    inner join 
    heroku_056eb661631f253.op_idea_vote op_idea_vote on op_ideas.idea_id = op_idea_vote.idea_id
group by
    op_ideas.author_id,
    op_ideas.date,
    op_ideas.deptid,
    op_ideas.idea_geo,
    op_ideas.idea_id,
    op_ideas.idea_text,
    op_ideas.idea_title,
    op_ideas.modifed_date,
    op_ideas.post_type,
    op_ideas.user_country,
    op_ideas.user_county,
    op_ideas.user_loc,
    op_organs.organ_name,
    op_organs.organ_logo,
    op_organs.active