在一段时间内计算记录

时间:2018-03-10 00:28:53

标签: mysql

我有一个池联赛比赛结果表,其中包含TIME类型的match_begin_time和match_end_time字段。

我想知道有多少记录属于不同的持续时间间隔,即< 1小时,1小时到1.5小时等等。

我尝试了以下代码来获取前2个区间,但每个区间都返回了所有记录,而不管Begin和End之间的时间间隔:

{

SELECT
     COUNT(TIMEDIFF(match_end_time, match_begin_time) BETWEEN time("00:00:00") AND time("00:59:59")) AS "< 1 Hour",
     COUNT(TIMEDIFF(match_end_time, match_begin_time) BETWEEN time("01:00:00") AND time("01:30:00")) AS "1 - 1.5 Hours"
FROM   lwljhb_lwl_matches
WHERE
     match_end_time > 0 AND
     match_end_time > 0 AND
     match_end_time > Match_begin_time
}

使用WHERE子句我可以为每个时间间隔运行一次查询,我得到了正确的答案。例如,以下代码正确返回持续时间为&lt; 4的记录。 1小时。

{

SELECT 
     TIMEDIFF(match_end_time, match_begin_time) AS "< 1 Hour"
FROM   lwljhb_lwl_matches
WHERE
     match_end_time > 0 AND
     match_end_time > 0 AND
     match_end_time > Match_begin_time AND
     TIMEDIFF(match_end_time, match_begin_time) BETWEEN time("00:00:00") AND time("00:59:59")
}

我的COUNT表达式在哪里出错?

2 个答案:

答案 0 :(得分:4)

我会给你一个简单的查询来显示落入时间间隔的记录数量'&lt; 1小时'和'1小时到1.5小时'。我认为这对你有帮助。

SELECT x.a AS '<1 Hour', y.b AS '1- 1.5 Hours'
FROM (SELECT COUNT(TIMEDIFF (match_end_time, match_begin_time)) AS a
       FROM lwljhb_lwl_matches 
       WHERE TIMEDIFF (match_end_time, match_begin_time)<'01:00:00') as x, 
     (SELECT COUNT(TIMEDIFF (match_end_time, match_begin_time)) AS b
       FROM lwljhb_lwl_matches 
       WHERE TIMEDIFF(match_end_time, match_begin_time) BETWEEN'01:00:00' AND '01:30:00') AS y

您可以使用COUNT(*)代替COUNT(TIMEDIFF (match_end_time, match_begin_time))生成相同的结果。 同样,您可以找到其他时间间隔的记录。您可以在SQLFiddle中查看我的代码。

答案 1 :(得分:1)

在MySQL中,我的COUNT中的布尔表达式产生1(真)或0(假)。 COUNT将这两者视为non_NULL并对其进行计数。我在思考(错误地)它只计算1(真实)。进一步的研究表明我有两种解决方法,使用COUNT&amp; IF或SUM。

COUNT(IF(TIMEDIFF(match_end_time, match_begin_time) BETWEEN time("00:00:00") AND time("00:59:59"), 1, NULL)) AS "
SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN time("00:00:00") AND time("00:59:59") AS "

The second eliminates the need for the IF because the booleans are treated as integers and the 1s (trues) increase the sum and the 0s (falses) don't. The NULLs are ignored.

So, my whole query is:

SELECT
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '00:00:01' AND '01:00:00' ) AS '<1 Hour',
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '01:00:01' AND '01:30:00' ) AS '1 - 1.5 Hours',
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '01:30:01' AND '02:00:00' ) AS '1.5 - 2 Hours',
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '02:00:01' AND '02:30:00' ) AS '2 - 2.5 Hours',
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '02:30:01' AND '03:00:00' ) AS '2.5 - 3 Hours',
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '03:00:01' AND '03:30:00' ) AS '3 - 3.5 Hours',
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '03:30:01' AND '04:00:00' ) AS '3.5 - 4 Hours',
     SUM(TIMEDIFF(match_end_time, match_begin_time) BETWEEN '04:00:01' AND '10:00:00' ) AS '> 4 Hours'
FROM lwljhb_lwl_matches