2013-05-03 112 views
0

我有一個具有多個ID爲給定的標準SQL表:MySQL的返回結果只有一個,而不是多個

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 fulfuls的一個過濾條件(其實他們都做):

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行而不是僅顯示第一行?

一個

+1

查找到'集團BY' – Lucas 2013-05-03 14:20:54

回答

0

countsum都是聚合功能。用group by條款,您可以按id(唯一)進行分組並顯示全部。

0

很難說沒有看到所有的表結構,但可以嘗試在一個GROUP BY在查詢的末尾:

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

沒有GROUP BY你指示的MySQL給你總計總數而不是ID總數。

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 
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;