Select 每个用户每天每个操作的最小时间戳 MySQL

Select mininum timestamp per user per day per action MySQL

我正在尝试获得每个用户组织的最小时间戳,然后是特定操作的日期。感谢您的帮助。

我研究了下面的代码,但不确定。

SELECT a1.*
FROM accesscards a1
JOIN (SELECT name, MIN(entrydates) mindate
      FROM accesscards
      WHERE name != ''
      GROUP BY name, date(entrydates)) a2
ON a1.name = a2.name AND a1.entrydates = a2.mindate

来自 Get records for each person's each day's min datetime

当前Table

+----------+------+--------+---------------------+
| idRecord | user | action | recorded            |
+----------+------+--------+---------------------+
| 1        | bob  | start  | 2018-01-27 01:00:01 |
+----------+------+--------+---------------------+
| 2        | amy  | close  | 2018-01-27 01:00:01 |
+----------+------+--------+---------------------+
| 3        | bob  | start  | 2018-01-27 02:00:01 |
+----------+------+--------+---------------------+
| 4        | amy  | start  | 2018-01-27 00:00:01 |
+----------+------+--------+---------------------+
| 5        | amy  | start  | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+
| 6        | jim  | open   | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+
| 7        | bob  | close  | 2018-01-27 02:00:01 |
+----------+------+--------+---------------------+
| 8        | bob  | start  | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+
| 9        | jim  | close  | 2018-01-28 00:00:01 |
+----------+------+--------+---------------------+

期望的结果

+------+----------+--------+---------------------+
| User | Date     | action | recorded            |
+------+----------+--------+---------------------+
| amy  | 01-27-18 | start  | 2018-01-27 00:00:01 |
+------+----------+--------+---------------------+
| amy  | 01-28-18 | start  | 2018-01-28 00:00:01 |
+------+----------+--------+---------------------+
| bob  | 01-27-18 | start  | 2018-01-28 01:00:01 |
+------+----------+--------+---------------------+
| bob  | 01-28-18 | start  | 2018-01-28 00:00:01 |
+------+----------+--------+---------------------+
| jim  | 01-28-18 | start  | 2018-01-28 00:00:01 |
+------+----------+--------+---------------------+

您需要根据 nameactiondate(entrydates)

进行分组
SELECT name, date(entrydates), action, MIN(entrydates)
FROM accesscards
WHERE action='start'
group by name, action, date(entrydates)

使用 WHERE action='start' 确保我们不对多个实例('start' 和 'close')进行分组,action 类型 start 总是最小值。