doudou3935 2011-07-26 10:50
浏览 39
已采纳

通过PHP或mySQL计算时间长度?

In my form users sumbit many events with individual times associated to each one.

e.g. For a Appendectomy the first event Event#1 began at 12:54:33pm and the last even Event#12 happened at 2:23:04pm. So for the Appendectomy there was 12 events.

These events are placed in a seperate table with a foreign key to the event name (Appendectomy).

So if I wanted to calculate the amount of time would it be better to do it through the database or using PHP.

I am unsure because of these reasons:

PHP:

The time formats are strings and I don't think you can subtract strings

The time formats are like this HH:MM:SS which may be complex to split the strings and reconcatenate them later.

MySQL:

The time formats in the the column are actual times not strings Would have to use mysql joins and then find the total events for that procedure. Which would be complex?

Stored procedure?

I am still new to alot of this so I was wondering what your guidance would be on this matter?

  • 写回答

1条回答 默认 最新

  • dongzaocheng3214 2011-07-26 11:00
    关注

    With SQL:

    SELECT procedure_id, SUBTIME(events_max.start, events_min.start) AS duration
    FROM procedure
    JOIN ( SELECT procedure_id, SELECT MAX(time) AS start FROM procedure_events GROUP BY procedure_id ) AS events_max USING ( procedure_id )
    JOIN ( SELECT procedure_id, SELECT MIN(time) AS start FROM procedure_events GROUP BY procedure_id ) AS events_min USING ( procedure_id )
    GROUP BY procedure_id
    

    Would give you the procedure IDs and duration of each procedure as given by the time difference between the start of the first event and the last event.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?