数据库查询计数所有是/否投票及其所属的db记录?

时间:2011-02-21 21:01:58

标签: mysql sql aggregate-functions

我有两个表,一个用于文章/民意调查,另一个用于对这些文章进行投票。投票在枚举字段中进行上/下,以及其他几个选项。我正在尝试创建一个查询,它将返回我需要的文章数据,并给我所有投票的总和。不幸的是,我得到了意想不到的结果。它记录了我的所有投票,好像它们是第一次进入的,而没有其他任何投票。

如何正确地将我的投票链接到他们所属的民意调查?

tblVotes
[PK] primaryId  |    voterId (FK)  | voteValue (enum) |    postID (fk)
----------------------------------------------------------------------
     1                    10                 up              1
     2                    11                 down            1
     3                    11                 up              10

tblContent
[PK] unique Id |   postTitle   |   postBody  |   postAuthor(FK)  |
------------------------------------------------------------------
       1               foo           foofoo          12
      10               bar           barbar          10
      11               foobar    foofoobarbar        10

当我调查下面详述的查询时,我希望我会得到这样的结果:

uniqueId |userName | pollDate | postTitle  |postBody  | upVotes| downVotes
--------------------------------------------------------------------------
   1         bob      1/1/11      foo        foofoo      1         1
  10         john     1/2/11      bar        barbar      1         null
  11         john     1/3/11      foofoo     foofoobar.. null      null

不幸的是,我实际上得到了这样的结果:

uniqueId |userName | pollDate | postTitle  |postBody  | upVotes| downVotes
--------------------------------------------------------------------------
   1         bob      1/1/11      foo        foofoo      2         1
  10         john     1/2/11      bar        barbar      null      null
  11         john     1/3/11      foofoo     foofoobar.. null      null

这是我的SQL语句:

            SELECT
                tblContent.uniqueID,
                tblUsers.userName,
                tblContent.postDate,
                tblContent.postTitle,
                tblContent.postBody,
                votes.upVotes,
                votes.downVotes
            FROM
                tblContent
                    LEFT JOIN
                    (
                        SELECT
                            postId,
                            SUM(CASE WHEN tblVotes.voteType = 'up' THEN 1 ELSE 0 END) as upVotes,
                            SUM(CASE WHEN tblVotes.voteType = 'down' THEN 1 ELSE 0 END) as downVotes
                        FROM
                            tblVotes, tblContent
                        WHERE
                            tblContent.uniqueId = tblVotes.postId
                     ) votes
                    ON tblContent.uniqueId = votes.postId
                    LEFT JOIN
                        tblUsers
                    ON tblUsers.userId = tblContent.postAuthor
            WHERE
                postApproved = true
            ORDER BY
                postDate DESC

2 个答案:

答案 0 :(得分:11)

使用:

   SELECT p.title,
          SUM(CASE WHEN v.votevalue = 'yes' THEN 1 ELSE 0 END) AS yesCount,
          SUM(CASE WHEN v.votevalue = 'no' THEN 1 ELSE 0 END) AS noCount
     FROM POLLS p
LEFT JOIN VOTES v ON v.pollid = p.uniqueid
 GROUP BY p.title

答案 1 :(得分:0)

嵌套SQL语句中需要GROUP BY