【问题标题】:Combining multiple rows a single row in a table将多行合并为表中的单行
【发布时间】:2021-06-08 14:37:42
【问题描述】:

所以我正在开发一个简单的 IN/OUT 板系统来跟踪谁进入和离开并抓拍他们的照片。

我让每个 IN/OUT 都进入一个访问表。

访问

id user_id datetime direction image
4 1 5/6/2021 16:04 In 1-1621019890.png
5 1 5/8/2021 13:25 Out 1-1621019891.png
9 1 5/10/2021 16:14 In 1-1621019892.png
12 1 5/11/2021 13:58 Out 1-1621019893.png
15 1 5/11/2021 14:02 In 1-1621019894.png
22 1 5/11/2021 14:31 Out 1-1621019895.png
25 1 5/11/2021 14:32 In 1-1621019896.png
26 1 5/11/2021 14:34 Out 1-1621019897.png
28 1 5/11/2021 15:02 In 1-1621019898.png
29 1 5/11/2021 15:04 Out 1-1621019899.png
32 1 5/11/2021 15:04 In 1-1621019880.png
33 1 5/11/2021 18:09 Out 1-1621019870.png
34 1 5/11/2021 19:05 In 1-1621019860.png
35 1 5/14/2021 19:16 Out 1-1621019808.png
38 1 5/14/2021 19:18 In 1-1621019888.png
39 1 5/14/2021 19:19 Out 1-1621019967.png

我正在尝试查找将输出类似以下内容的 SQL 查询:

User_id In in-image Out out-image Total Time
1 5/6/2021 16:04 1-1621019890.png 5/8/2021 13:25 1-1621019891.png
1 5/10/2021 16:14 1-1621019892.png 5/11/2021 13:58 1-1621019893.png
1 5/11/2021 14:02 1-1621019894.png 5/11/2021 14:31 1-1621019895.png
1 5/11/2021 14:32 1-1621019896.png 5/11/2021 14:34 1-1621019897.png
1 5/11/2021 15:02 1-1621019898.png 5/11/2021 15:04 1-1621019899.png
1 5/11/2021 15:04 1-1621019880.png 5/11/2021 18:09 1-1621019870.png
1 5/11/2021 19:05 1-1621019860.png 5/14/2021 19:16 1-1621019808.png
1 5/14/2021 19:18 1-1621019888.png 5/14/2021 19:19 1-1621019967.png

如果我也能用它来计算总时间就好了,但在最坏的情况下我可以在 PHP 中做到这一点。

通过大量搜索,我似乎需要在我的 MySQL 查询中设置 PIVOT,但没有看到如何使用多个列进行设置。

【问题讨论】:

  • 如何关联/分组特定的进出行?在真实数据中,user_id 会有所不同吗?
  • 以上只是我执行 SELECT * FROM 访问 WHERE user_id=1 的地方,因此我可以查看该特定用户的所有输入/输出。但是在实际的数据库中,您将有多个人进出。 ---- 我猜你是对的,也许我需要一个包含 3 列 ID 的组表 | in_id | out_id 然后我可以做一个连接将它们链接在一起。

标签: php mysql sql pivot


【解决方案1】:

如果您的数据是常规的(没有遗漏 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

【讨论】:

    【解决方案2】:

    感谢 David784 的指导。

    我最终创建了一个名为 accessgroups 的新表,它只有 2 列 in_id 和 out_id。这将允许我将 2 个访问条目组合在一起。然后我可以使用它来执行 SQL 连接,以获得我想要的输出。

    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
    

    现在我只需要修改我的 web 应用程序,以便在您登录时在 accessgroups 中创建该行并使用注销 ID 更新它。我将只使用 PHP 来计算 IN/OUT 时间的差异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多