0
SELECT
    HOUR (`Timestamp`) AS HOUR,
    COUNT(*) AS Alarms

FROM
    `alarms`
WHERE
    `Siteindex` IN ('4, 5, 8, 10, 11, 15')
AND `Datestamp` = '2012-11-07'
GROUP BY
    HOUR (`Timestamp`);

此代码仅在每个 id 来自IN ('4, 5, 8, 10, 11, 15')have时才有效Datestamp = '2012-11-07',但如何仅计算那些拥有它的人?

例如上面的代码返回NULL是因为 2012-11-07 中只有 15 行有一些行,但我需要它不理会其他行并返回所有

4

3 回答 3

1

您的where条款很可能是问题所在

WHERE `Siteindex` IN ('4, 5, 8, 10, 11, 15')

根据 的类型siteindex,您应该将其更改为

WHERE `Siteindex` IN (4, 5, 8, 10, 11, 15)

或者

WHERE `Siteindex` IN ('4', '5', '8', '10', '11', '15')
于 2012-12-16T18:07:52.617 回答
0

我不完全清楚你在问什么,但如果你只是想计算有多少行符合你的搜索条件,我会尝试撒谎:

SELECT COUNT(*)
FROM alarms
WHERE Siteindex IN ('4,5,8,10,11,15') AND Datestamp = '2012-11-07';
于 2012-12-16T17:47:39.017 回答
0

尝试这个

      SELECT  HOUR (`Timestamp`) AS HOUR,

  COUNT(Siteindex) AS Al

 FROM ( SELECT Siteindex FROM alarms WHERE `Siteindex` IN (4, 5, 8, 10, 11, 15))
`alarms`
 WHERE
  `Datestamp` = '2012-11-07'
GROUP BY
HOUR ;

这将只计算您拥有的那些 ID。

于 2012-12-16T18:11:34.163 回答