0

我有一个 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 行,而不仅仅是第一行?

一个

4

4 回答 4

0

如果不查看所有表结构,很难说,但请尝试GROUP BY在查询末尾添加 a:

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

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

于 2013-05-03T14:21:33.717 回答
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
于 2013-05-03T14:21:50.890 回答
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;
于 2013-05-03T14:23:50.270 回答
0

count并且sum都是聚合函数。使用group by子句,您可以按id(唯一)分组并显示全部。

于 2013-05-03T14:22:30.843 回答