0

我目前正在尝试进行 mysql 查询,该查询将按以下方式计算每个项目的零和一的数量:表:

    ID    |    PollID    |    Value
------------------------------------
    1     |      1       |     1
    2     |      1       |     1
    3     |      2       |     0
    4     |      2       |     1
    5     |      1       |     0

我想要的结果是:

    Poll    |    one    |    zero
----------------------------------
      1     |     2     |     1
      2     |     1     |     1

谢谢您的帮助!

4

3 回答 3

1

这是可能的最短答案,MySQL因为它支持布尔算术。

SELECT  PollID,
        SUM(value = 1) AS `One`,
        SUM(value = 0) AS `Zero`
FROM    tableName
GROUP   BY PollID
于 2013-03-23T13:13:33.413 回答
0

尝试这个

      select table.pollid,
      Switch(table.value Like 1, 1)AS one,
      Switch(table.value Like 0, 1)AS zero       
      from table
      group by pollid
于 2013-03-23T13:10:56.030 回答
0
select z.pollid,z.ones,s.zeros 
  from (select a.pollid,count(a.value) as ones from test a
    where a.value=1
group by a.pollid) z   
left join   
    (select b.pollid,count(b.value) as zeros from test b  
     where b.value=0 group by b.pollid) s    
on z.pollid=s.pollid;
于 2013-03-23T13:13:29.677 回答