简体   繁体   English

将多行合并为表中的单行

[英]Combining multiple rows a single row in a table

So I'm working on a simple IN/OUT board system to keep track of who enters and leaves and grabs a picture of them.所以我正在开发一个简单的 IN/OUT 板系统来跟踪谁进入和离开并抓拍他们的照片。

I have each IN/OUT going to an access table.我让每个 IN/OUT 进入访问表。

access使用权

id ID user_id用户身份 datetime约会时间 direction方向 image图片
4 4 1 1 5/6/2021 16:04 2021 年 5 月 6 日 16:04 In 1-1621019890.png 1-1621019890.png
5 5 1 1 5/8/2021 13:25 2021 年 5 月 8 日 13:25 Out出去 1-1621019891.png 1-1621019891.png
9 9 1 1 5/10/2021 16:14 2021 年 5 月 10 日 16:14 In 1-1621019892.png 1-1621019892.png
12 12 1 1 5/11/2021 13:58 2021 年 5 月 11 日 13:58 Out出去 1-1621019893.png 1-1621019893.png
15 15 1 1 5/11/2021 14:02 2021 年 5 月 11 日 14:02 In 1-1621019894.png 1-1621019894.png
22 22 1 1 5/11/2021 14:31 2021 年 5 月 11 日 14:31 Out出去 1-1621019895.png 1-1621019895.png
25 25 1 1 5/11/2021 14:32 2021 年 5 月 11 日 14:32 In 1-1621019896.png 1-1621019896.png
26 26 1 1 5/11/2021 14:34 2021 年 5 月 11 日 14:34 Out出去 1-1621019897.png 1-1621019897.png
28 28 1 1 5/11/2021 15:02 2021 年 5 月 11 日 15:02 In 1-1621019898.png 1-1621019898.png
29 29 1 1 5/11/2021 15:04 2021 年 5 月 11 日 15:04 Out出去 1-1621019899.png 1-1621019899.png
32 32 1 1 5/11/2021 15:04 2021 年 5 月 11 日 15:04 In 1-1621019880.png 1-1621019880.png
33 33 1 1 5/11/2021 18:09 2021 年 5 月 11 日 18:09 Out出去 1-1621019870.png 1-1621019870.png
34 34 1 1 5/11/2021 19:05 2021 年 5 月 11 日 19:05 In 1-1621019860.png 1-1621019860.png
35 35 1 1 5/14/2021 19:16 2021 年 5 月 14 日 19:16 Out出去 1-1621019808.png 1-1621019808.png
38 38 1 1 5/14/2021 19:18 2021 年 5 月 14 日 19:18 In 1-1621019888.png 1-1621019888.png
39 39 1 1 5/14/2021 19:19 2021 年 5 月 14 日 19:19 Out出去 1-1621019967.png 1-1621019967.png

I'm trying to locate a SQL query that will output something similar to this:我试图找到一个 SQL 查询,它将 output 类似于此:

User_id用户身份 In in-image图像内 Out出去 out-image外像 Total Time总时间
1 1 5/6/2021 16:04 2021 年 5 月 6 日 16:04 1-1621019890.png 1-1621019890.png 5/8/2021 13:25 2021 年 5 月 8 日 13:25 1-1621019891.png 1-1621019891.png
1 1 5/10/2021 16:14 2021 年 5 月 10 日 16:14 1-1621019892.png 1-1621019892.png 5/11/2021 13:58 2021 年 5 月 11 日 13:58 1-1621019893.png 1-1621019893.png
1 1 5/11/2021 14:02 2021 年 5 月 11 日 14:02 1-1621019894.png 1-1621019894.png 5/11/2021 14:31 2021 年 5 月 11 日 14:31 1-1621019895.png 1-1621019895.png
1 1 5/11/2021 14:32 2021 年 5 月 11 日 14:32 1-1621019896.png 1-1621019896.png 5/11/2021 14:34 2021 年 5 月 11 日 14:34 1-1621019897.png 1-1621019897.png
1 1 5/11/2021 15:02 2021 年 5 月 11 日 15:02 1-1621019898.png 1-1621019898.png 5/11/2021 15:04 2021 年 5 月 11 日 15:04 1-1621019899.png 1-1621019899.png
1 1 5/11/2021 15:04 2021 年 5 月 11 日 15:04 1-1621019880.png 1-1621019880.png 5/11/2021 18:09 2021 年 5 月 11 日 18:09 1-1621019870.png 1-1621019870.png
1 1 5/11/2021 19:05 2021 年 5 月 11 日 19:05 1-1621019860.png 1-1621019860.png 5/14/2021 19:16 2021 年 5 月 14 日 19:16 1-1621019808.png 1-1621019808.png
1 1 5/14/2021 19:18 2021 年 5 月 14 日 19:18 1-1621019888.png 1-1621019888.png 5/14/2021 19:19 2021 年 5 月 14 日 19:19 1-1621019967.png 1-1621019967.png

It would be great if I could also get it to calculate the total time in but worst case I can do that in PHP.如果我也能用它来计算总时间,那就太好了,但在最坏的情况下,我可以在 PHP 中做到这一点。

From doing a lot of searching it would appear that I would need to setup a PIVOT in my MySQL query but not seeing how to do that with multiple columns.通过大量搜索,我似乎需要在我的 MySQL 查询中设置一个 PIVOT ,但看不到如何使用多列进行设置。

Provided your data are regular (no missihng Out row for an In row)如果您的数据是常规的(In 行没有遗漏 Out 行)

select user_id, 
   max(case when direction='In' then datetime end) in,
   max(case when direction='In' then image end) in_image,
   max(case when direction='Out' then datetime end) out,
   max(case when direction='Out' then image end)    out_image
from (select *,
         row_number() over(partition by user_id, direction order by datetime) rn
      from yourtable) t
group by user_id, rn

Thanks to David784 for the direction.感谢 David784 的指导。

I ended up creating a new table called accessgroups which just has 2 columns in_id and out_id.我最终创建了一个名为 accessgroups 的新表,它只有 2 列 in_id 和 out_id。 This will allow me to group together 2 access entries.这将允许我将 2 个访问条目组合在一起。 I can then use that to do a SQL join to get the output exactly how I'd like.然后我可以用它来做一个 SQL 加入得到 output 正是我想要的。

SELECT access.user_id,access.datetime as 'IN',access.image as 'IN_IMAGE',(SELECT datetime from access WHERE id = accessgroup.out_id) as 'OUT',(SELECT image from access WHERE id = accessgroup.out_id) as 'OUT_IMAGE' 
FROM `access` 
INNER JOIN accessgroup 
ON access.id = accessgroup.in_id 
WHERE direction = 1

Now I just have to modify my webapp to create that row in accessgroups when you sign in and update it with the signout id.现在我只需要修改我的 webapp 以在您登录时在 accessgroups 中创建该行并使用注销 ID 更新它。 And I'll just use PHP to calculate the difference in IN/OUT times.我将只使用 PHP 来计算输入/输出时间的差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM