mysql - 使用 MySQL 中的 Join 获取正确的时差

标签 mysql join

我在使用 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/

相关文章:

mysql - 查询以获取自上次登录以来的所有用户事件

mysql - Group By 子句不显示相同的行值?

mysql - 选择具有重复数据的行

sql - SQL JOIN 查询中的重复条目

sql - 查询插入和选择说明

mysql - Action Controller::Connection 在使用 mysql 在 ruby​​ on rails 中创建应用程序时未建立

php - 从 php 表行传递参数

mysql - MySQL View 上的全文索引或更正在连接表中搜索的方式

MYSQL 从同一列中选择多个值作为一行

MySQL 多连接表