mysql只返回一个结果而不是多个结果

时间:2013-05-03 14:18:23

标签: mysql

我有一个sql表,它对于给定的条件有多个id:

mysql> select distinct id from FILTER where ft='f' and timestamp between '1367539200000' and '1367625599999';
+-----+
| id  |
+-----+
| 0   |
| 121 |
| 122 |
| 124 |
| 125 |
| 127 |
+-----+
6 rows in set (0.00 sec)

mysql>

我想运行一个查询,为所有6行提供结果:

mysql> select a.id, count(a.id) as total, SUM(CASE WHEN b.result='0' THEN 1 ELSE 0 END) AS allowed, SUM(CASE WHEN b.result='1' THEN 1 ELSE 0 END) AS modified, SUM(CASE WHEN b.result='2' THEN 1 ELSE 0 END) AS blocked from FILTER a INNER JOIN EVENTS b on a.fid = b.event_id and a.timestamp = b.timestamp where b.protocol_type='MM4' and a.ft='f' and a.id in ('0', '121', '122', '124', '125', '127') and a.fid=b.event_id and b.timestamp between '1367539200000' and '1367625599999';
+----+-------+---------+----------+---------+
| id | total | allowed | modified | blocked |
+----+-------+---------+----------+---------+
| 0  |   216 |       0 |      135 |      81 |
+----+-------+---------+----------+---------+
1 row in set (0.01 sec)

mysql>

这只返回第一行。我可以验证其中一个其他ID是否符合过滤条件(实际上它们都是这样):

mysql> select a.id, count(a.id) as total, SUM(CASE WHEN b.result='0' THEN 1 ELSE 0 END) AS allowed, SUM(CASE WHEN b.result='1' THEN 1 ELSE 0 END) AS modified, SUM(CASE WHEN b.result='2' THEN 1 ELSE 0 END) AS blocked from FILTER a INNER JOIN EVENTS b on a.fid = b.event_id and a.timestamp = b.timestamp where b.protocol_type='MM4' and a.ft='f' and a.id='127' and a.fid=b.event_id and b.timestamp between '1367539200000' and '1367625599999';
+-----+-------+---------+----------+---------+
| id  | total | allowed | modified | blocked |
+-----+-------+---------+----------+---------+
| 127 |    24 |       0 |       15 |       9 |
+-----+-------+---------+----------+---------+
1 row in set (0.00 sec)

mysql>

我也试过没有列表:

mysql> select a.id, count(a.id) as total, SUM(CASE WHEN b.result='0' THEN 1 ELSE 0 END) AS allowed,  SUM(CASE WHEN b.result='1' THEN 1 ELSE 0 END) AS modified, SUM(CASE WHEN b.result='2' THEN 1 ELSE 0 END) AS blocked from FILTER a INNER JOIN EVENTS b on a.fid = b.event_id and a.timestamp = b.timestamp where b.protocol_type='MM4' and a.ft='f' and a.fid=b.event_id and b.timestamp between '1367539200000' and '1367625599999';
+----+-------+---------+----------+---------+
| id | total | allowed | modified | blocked |
+----+-------+---------+----------+---------+
| 0  |   216 |       0 |      135 |      81 |
+----+-------+---------+----------+---------+
1 row in set (0.00 sec)

mysql>

如何更改我的过滤器以显示每个ID的6行而不仅仅是第一行?

A

4 个答案:

答案 0 :(得分:0)

如果没有看到所有的表结构,很难说,但尝试在查询结束时添加GROUP BY

... your query as posted, with ...
GROUP BY a.id

如果没有GROUP BY,您指示MySQL为您提供总计,而不是按ID计算总数。

答案 1 :(得分:0)

select a.id, 
count(a.id) as total, 
SUM(CASE WHEN b.result='0' THEN 1 ELSE 0 END) AS allowed, 
SUM(CASE WHEN b.result='1' THEN 1 ELSE 0 END) AS modified, 
SUM(CASE WHEN b.result='2' THEN 1 ELSE 0 END) AS blocked 
from FILTER a INNER JOIN EVENTS b on a.fid = b.event_id and a.timestamp = b.timestamp 
where b.protocol_type = 'MM4' 
and a.ft = 'f' 
and a.fid = b.event_id 
and b.timestamp between '1367539200000' and '1367625599999'
GROUP BY a.id

答案 2 :(得分:0)

countsum都是聚合函数。使用group by子句,您可以按id(唯一)进行分组并显示所有内容。

答案 3 :(得分:0)

这样的事情:

select 
    a.id, 
    count(a.id) as total, 
    SUM(
        CASE WHEN b.result='0' THEN 1 
        ELSE 0 
        END
    ) AS allowed, 
    SUM(
        CASE WHEN b.result='1' THEN 1 
        ELSE 0 
        END
    ) AS modified, 
    SUM(
        CASE WHEN b.result='2' THEN 1 
        ELSE 0 
        END
    ) AS blocked 
from FILTER a 
    INNER JOIN EVENTS b on a.fid = b.event_id 
        and a.timestamp = b.timestamp 
where b.protocol_type='MM4' 
    and a.ft='f' 
    and a.id='127' 
    and a.fid=b.event_id 
    and b.timestamp between '1367539200000' and '1367625599999'
group by a.id;