如何将mysql行转换为没有标签的列

时间:2019-04-23 11:37:58

标签: mysql

我有一个带有行的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" |
+--------+--------------+-----------------------+-----------------------+

谢谢。

1 个答案:

答案 0 :(得分:0)

min()max()group by一起使用

select Userid , Date, min(`time`) as intime,max(`time`) as outtime
from tablename
group by Userid , Date