-2

我的问题是我想在单个查询中获得三个条件的计数。我知道我可以通过运行多个查询来做到这一点,但问题是我在一个查询中需要它

我尝试过的查询是

SELECT COUNT( IF( da <3, da, 0 ) ) AS da1, 
       COUNT( IF( da <2, da, 0 ) ) AS da2, 
       COUNT( IF( da <1, da, 0 ) ) AS da3
FROM tv

它的结果是

4 4 4

但预期的结果是

2 1 0

列表中存在的数据da

3 2 1 3
4

3 回答 3

2
SELECT SUM( IF( rr <3, 1, 0 ) ) AS da1, 
       SUM( IF( rr <2, 1, 0 ) ) AS da2, 
       SUM( IF( rr <1, 1, 0 ) ) AS da3
FROM tv;
于 2013-03-21T05:47:31.747 回答
0

COUNT()需要与GROUP BY- 我认为这可能是您的问题,请尝试按“da”分组。

于 2013-03-21T05:49:25.017 回答
0

我刚刚为我的问题找到了解决方案,但PSR 的解决方案也是正确的

SELECT * 
FROM (

    SELECT COUNT( da ) AS da1
    FROM tv
    WHERE da <3
    ) AS da1, (

    SELECT COUNT( da ) AS da2
    FROM tv
    WHERE da <2
    ) AS da2, (

    SELECT COUNT( da ) AS da3
    FROM tv
    WHERE da <1
    ) AS da3
于 2013-03-21T05:53:40.550 回答