我在使用 mysql 连接获取时差时遇到一些问题。我用最小和最大的时间差来玩,但无法让它工作。感谢您的帮助。
SELECT DISTINCT
ab.record AS `record`,
ab.timeIn AS `timeIN`,
de.timeOut AS `timeOut`,
TIMEDIFF(de.timeOut,ab.timeIn) AS `timeDifference`,
ab.description AS `description`,
FROM
TableA ab
INNER JOIN TableB de ON de.record = ab.record
GROUP BY `timeIn`, `description`
这是表A
+----+--------+---------------------+-------------+
| id | record | timeIn | description |
+----+--------+---------------------+-------------+
| 1 | 9 | 2017-03-25 12:41:59 | productA |
+----+--------+---------------------+-------------+
| 2 | 11 | 2017-03-25 15:00:35 | productB |
+----+--------+---------------------+-------------+
| 4 | 11 | 2017-03-25 12:42:00 | productB |
+----+--------+---------------------+-------------+
| 5 | 14 | 2017-03-25 12:42:00 | productC |
+----+--------+---------------------+-------------+
这是表B
+----+--------+---------------------+-------------+
| id | record | timeOut | description |
+----+--------+---------------------+-------------+
| 1 | 11 | 2017-03-25 15:10:10 | productB |
+----+--------+---------------------+-------------+
| 2 | 9 | 2017-03-25 12:47:17 | productA |
+----+--------+---------------------+-------------+
| 4 | 11 | 2017-03-25 12:47:17 | productB |
+----+--------+---------------------+-------------+
| 5 | 14 | 2017-03-25 12:47:18 | productC |
+----+--------+---------------------+-------------+
与 MySQL 时差的组合表
+----+--------+---------------------+---------------------+----------------+-------------+
| id | record | timeIn | timeOut | timeDifference | description |
+----+--------+---------------------+---------------------+----------------+-------------+
| 1 | 9 | 2017-03-25 12:41:59 | 2017-03-25 12:47:17 | 00:05:18 | productA |
+----+--------+---------------------+---------------------+----------------+-------------+
| 2 | 11 | 2017-03-25 12:42:00 | 2017-03-25 15:10:10 | 02:28:10 | productB |
+----+--------+---------------------+---------------------+----------------+-------------+
| 4 | 14 | 2017-03-25 12:42:00 | 2017-03-25 12:47:18 | 00:05:18 | productC |
+----+--------+---------------------+---------------------+----------------+-------------+
| 5 | 11 | 2017-03-25 15:00:35 | 2017-03-25 15:10:10 | 00:09:35 | productB |
+----+--------+---------------------+---------------------+----------------+-------------+
期望的结果是什么:
+----+--------+---------------------+---------------------+----------------+-------------+
| id | record | timeIn | timeOut | timeDifference | description |
+----+--------+---------------------+---------------------+----------------+-------------+
| 1 | 9 | 2017-03-25 12:41:59 | 2017-03-25 12:47:17 | 00:05:18 | productA |
+----+--------+---------------------+---------------------+----------------+-------------+
| 2 | 11 | 2017-03-25 12:42:00 | 2017-03-25 12:47:17 | 00:05:18 | productB |
+----+--------+---------------------+---------------------+----------------+-------------+
| 4 | 14 | 2017-03-25 12:42:00 | 2017-03-25 12:47:18 | 00:05:18 | productC |
+----+--------+---------------------+---------------------+----------------+-------------+
| 5 | 11 | 2017-03-25 15:00:35 | 2017-03-25 15:10:10 | 00:09:35 | productB |
+----+--------+---------------------+---------------------+----------------+-------------+
最佳答案
如果每条记录有多个值
您应该使用聚合函数作为 min 和 mx 来获取第一个和最后一个值,然后您应该将 bu 分组
SELECT
ab.record AS `record`,
min(ab.timeIn )AS `timeIN`,
max(de.timeOut ) AS `timeOut`,
TIMEDIFF(max(de.timeOut),min(ab.timeIn)) AS `timeDifference`,
ab.description AS `description`,
FROM
TableA ab
INNER JOIN TableB de ON de.record = ab.record
GROUP BY record
关于mysql - 使用 MySQL 中的 Join 获取正确的时差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031933/