PHP从具有ID的薄荷列表中计算开始时间和结束时间

时间:2010-11-02 20:29:37

标签: php sql datetime loops

抱歉再次打扰。我搜索过我最好的,到处看,仍然无法解决问题。能否请你帮忙? - 我会尽力解释这个问题。

问题: 我有一个带有ID的时间列表,我想在ID中循环这些时间以获得开始时间和结束时间。这是一个例子:

   time_id           time_duration

 1   2010-10-15 11:15:00
 1   2010-10-15 11:20:00
 1   2010-10-15 11:25:00
 1   2010-10-15 11:30:00
 2   2010-10-15 11:35:00
 2   2010-10-15 11:40:00
 2   2010-10-15 11:45:00
 2   2010-10-15 11:50:00
 2   2010-10-15 11:55:00
 3   2010-10-15 12:00:00
 3   2010-10-15 12:05:00
 4   2010-10-15 12:10:00
 5   2010-10-15 12:15:00
 5   2010-10-15 12:20:00
 5   2010-10-15 12:25:00

所以结果应该回应:

   for time_id 1: "Start 11:15  -  End 11:30"
   for time_id 2: "Start 11:35  -  End 11:55"
   for time_id 3: "Start 12:00  -  End 12:05"
   for time_id 4: "Start 12:10  -  End 12:10"
   for time_id 5: "Start 12:15  -  End 12:25"

这可能吗?请帮忙。我从早上起就一直在这。我尝试使用for循环但我一直遇到奇怪的错误。

谢谢编程家伙! :)

2 个答案:

答案 0 :(得分:2)

SELECT 
    time_id, 
    MIN(time_duration) AS start_time, 
    MAX(time_duration) AS end_time
FROM 
    abc_table 
GROUP BY 
    time_id;

那应该做的。

答案 1 :(得分:1)

试试这个。也许它可以帮到你。你只需要改变一下我想:

 select concat_ws("","for time_id ","",time_id," ",min(time_duration)," - ",max(time_duration)) as result from table1 group by time_id;

它会给你这个结果:

+---------------------------------------------------------+
| result                                                  |
+---------------------------------------------------------+
| for time_id 1 2010-10-15 11:15:00 - 2010-10-15 11:30:00 |
| for time_id 2 2010-10-15 11:35:00 - 2010-10-15 11:55:00 |
+---------------------------------------------------------+

编辑:只需添加您需要的内容:

mysql> select concat_ws("","for time_id ","",time_id," ",min(TIME_FORMAT(time_duration,'%h:%i:%s'))," - ",max(TIME_FORMAT(time_duration,'%h:%i:%s'))) as result,
    -> DATE_FORMAT(time_duration,'%d-%m-%Y') as date
    -> from table1
    -> group by time_id;


+-----------------------------------+------------+
| result                            | date       |
+-----------------------------------+------------+
| for time_id 1 11:15:00 - 11:30:00 | 15-10-2010 |
| for time_id 2 11:35:00 - 11:55:00 | 15-10-2010 |
+-----------------------------------+------------+