php - 计算时间长度,通过PHP还是mySQL?

标签 php mysql codeigniter

在我的表单中,用户提交了许多事件以及与每个事件相关的单独时间。

例如对于阑尾切除术,第一个事件 Event#1 于下午 12:54:33 开始,最后一个事件 Event#12 发生于下午 2:23:04。 因此,对于阑尾切除术,有 12 个事件。

这些事件被放置在一个单独的表中,并带有事件名称的外键(阑尾切除术)。

所以如果我想计算时间量,通过数据库或使用 PHP 会更好。

由于以下原因,我不确定:

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?

存储过程?

我对很多事情还是陌生的,所以我想知道你对这个问题的指导是什么?

最佳答案

使用 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

将为您提供过程 ID 和每个过程的持续时间,由第一个事件和最后一个事件开始之间的时间差给出。

关于php - 计算时间长度,通过PHP还是mySQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6828915/

相关文章:

php - php 中的 global 与 $GLOBALS

将 JSON 代码写入 txt 文件的 PHP 脚本缺少空格和换行符。怎么修?

mysql - 在后端阻止连续的 SQL 请求

MySQL管道分隔值与映射表

php - 如何在 php 中使用 oauth token 和 secret 发推文

javascript - 如何在Codeigniter中序列化文件上传字段?

php - 跟踪用户在页面上停留的时间?

php - Symfony 2 - 如何在我自己的 Yaml 文件加载器中解析 %parameter%?

python - SQL 用于组合字段中的数据并提供整理后的数据

codeigniter - Codeigniter 事件记录中的子查询