我有一个带有行的mysql表,该表基本上是用户的进出时间,但是它没有任何标签,其中行是In,行是Out。
找出的唯一方法是为每个用户获取每个日期的最短和最长时间,然后在两列中进行选择。但是我很难做到这一点..请帮助
My current data looks like this:
+-----------+--------------+-----------------------+
| "User Id" | Date | "Time" |
+-----------+--------------+-----------------------+
| userA | "2019-04-22" | "2019-04-22 08:48:23" |
+-----------+--------------+-----------------------+
| userA | "2019-04-22" | "2019-04-22 17:47:51" |
+-----------+--------------+-----------------------+
| userB | "2019-04-22" | "2019-04-22 08:19:52" |
+-----------+--------------+-----------------------+
| userB | "2019-04-22" | "2019-04-22 18:06:27" |
+-----------+--------------+-----------------------+
| userC | "2019-04-22" | "2019-04-22 08:27:28" |
+-----------+--------------+-----------------------+
| userc | "2019-04-22" | "2019-04-22 17:19:25" |
+-----------+--------------+-----------------------+
Desired Output is This:
+--------+--------------+-----------------------+-----------------------+
| Userid | Date | InTime | OutTime |
+--------+--------------+-----------------------+-----------------------+
| userA | "2019-04-22" | "2019-04-22 08:48:23" | "2019-04-22 17:47:51" |
+--------+--------------+-----------------------+-----------------------+
| userB | "2019-04-22" | "2019-04-22 08:19:52" | "2019-04-22 18:06:27" |
+--------+--------------+-----------------------+-----------------------+
| userC | "2019-04-22" | "2019-04-22 08:27:28" | "2019-04-22 17:19:25" |
+--------+--------------+-----------------------+-----------------------+
提前致谢。
最佳答案
将min()
和max()
与group by
一起使用
select Userid , Date, min(`time`) as intime,max(`time`) as outtime
from tablename
group by Userid , Date