如何添加匹配 mysql 查询的小时数?

How to add hours matching a mysql query?

数据(Table名称:T1):

Teacher Subject Day Hour
Albert Blue Wednesday 10:00
Albert Blue Wednesday 12:00
Brandon Red Tuesday 09:00
Brandon Red Tuesday 11:00
Albert Cyan Monday 08:30
Albert Cyan Monday 10:30
Claudia Gray Thursday 08:00
Claudia Gray Thursday 10:00
Albert Pink Friday 13:00
Albert Pink Friday 14:30
Martha Green Wednesday 12:00
Martha Green Wednesday 14:00
Albert Yellow Friday 11:00
Albert Yellow Friday 12:30

可以看出,一个Subject的开始时间有一条记录,同一个Subject的结束时间有一条记录(数据是这样来的)。 我想知道的是特定老师每周花在 类 上的时间,比方说“Albert”。

查询:

$result = mysqli_query($link, "SELECT SUBTIME(max(Hour), min(Hour)) AS TeachTime, Subject FROM T1 WHERE Teachers LIKE '%Albert%' GROUP BY Subject ORDER BY Subject ASC") or die(mysqli_error($link));
if (mysqli_num_rows($result)!==0) {
echo "Weekly teaching time: ";
while($row = mysqli_fetch_array($result)){
echo $row['TeachTime']." Hrs. ";
}
}

输出:

每周教学时间:02:00 小时。 02:00 小时。 01:30 小时。 01:30小时

期望的输出:

每周教学时间:07:00小时

如您所见,我不知道如何对每个结果小时数进行加法运算。我怎样才能做到这一点?

我也试过GROUP BY Teachers但是结果很奇怪...不是相加的结果

你知道如何得到这个:

mysql> select teacher, day, min(hour), max(hour) 
from T1 where teacher = 'Albert' group by teacher, day;
+---------+-----------+-----------+-----------+
| teacher | day       | min(hour) | max(hour) |
+---------+-----------+-----------+-----------+
| Albert  | Wednesday | 10:00:00  | 12:00:00  |
| Albert  | Monday    | 08:30:00  | 10:30:00  |
| Albert  | Friday    | 11:00:00  | 14:30:00  |
+---------+-----------+-----------+-----------+

您可以使用TIMESTAMPDIFF()来计算分钟数:

mysql> select teacher, day, 
timestampdiff(minute, min(hour), max(hour)) as minutes 
from T1 where teacher = 'Albert' group by teacher, day;
+---------+-----------+---------+
| teacher | day       | minutes |
+---------+-----------+---------+
| Albert  | Wednesday |     120 |
| Albert  | Monday    |     120 |
| Albert  | Friday    |     210 |
+---------+-----------+---------+

现在将其包装为另一个聚合查询中的子查询:

mysql> select teacher, sum(minutes) as total_minutes 
from (
  select teacher, day, 
  timestampdiff(minute, min(hour), max(hour)) as minutes 
  from T1 where teacher = 'Albert' group by teacher, day) as t 
group by teacher;
+---------+---------------+
| teacher | total_minutes |
+---------+---------------+
| Albert  |           450 |
+---------+---------------+

这是我最终使用的代码,感谢 Bill Karwin 的指导和更多的研究:

$result = mysqli_query($link, "SELECT *,
SEC_TO_TIME(SUM(TIME_TO_SEC(WeekTime))) AS ProfTime FROM
(SELECT *,
SUBTIME(max(Hour), min(Hour)) AS WeekTime FROM T1
WHERE Teacher LIKE '%Albert%' GROUP BY Teacher, Subject) AS C")
or die(mysqli_error($link));
if (mysqli_num_rows($result)!==0) {
echo "Weekly teaching time: ";
while($row = mysqli_fetch_array($result)){
echo date('H:i', strtotime($row['ProfTime']))." Hrs. <br>";
}}

Bill 的回答让我意识到您可以从嵌入式查询中查询结果,例如:

Query2 over 结果(Query1 over data)

然后,由于 query1 的结果是时间格式 (HH:MM),我应该得到所有检索到的实例的总和,即 01:00 + 02:00 + 01:30 = 04:30

添加必须在查询 2 中执行,我在 tutorialspoint 上发现查询 SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(Time))) AS TotalTime 执行此操作。

最后,由于结果采用 HH:MM:SS 格式,我只需要 HH:MM 我应用了php 转换为输出:date('H:i', strtotime($row['TotalTime'])).