【问题标题】:Convert selected column values to another row using select with the same id [duplicate]使用具有相同 id 的 select 将选定的列值转换为另一行 [重复]
【发布时间】:2013-12-19 00:52:58
【问题描述】:

我正在制作一个简单的进出系统。我有 3 对进出。 p_id(person_id)

表A

p_id  time_id      status            timestamp
1         1           in        2013-12-18 15:44:09
2         2           in        2013-12-18 16:23:19
1         3           out       2013-12-18 18:31:11
1         4           in        2013-12-18 18:50:11
3         5           out       2013-12-18 19:20:16
1         6           out       2013-12-18 19:50:11
2         7           out       2013-12-18 19:51:19
1         8           in        2013-12-19 07:51:19
1         9           out       2013-12-19 12:00:19
1         10          in        2013-12-19 01:00:19
1         11          out       2013-12-19 05:30:19
1         12          in        2013-12-19 07:51:19
1         13          out       2013-12-19 11:00:19

如何将表格选择到表格结果中(同一日期的一行,右侧的输入和输出对根据 time_id 升序)?请检查这个简单的例子,也许你有一个想法。 http://www.sqlfiddle.com/#!2/6dd1f/8

p_id status     timestamp        status     timestamp       status     timestamp          status      timestamp          status     timestamp       status      timestamp
1    in   2013-12-18 15:44:09   out   2013-12-18 18:31:11  in    2013-12-18 18:50:11    out     2013-12-18 19:50:11
2    in   2013-12-18 16:23:19   out   2013-12-18 19:51:19  
3                               out   2013-12-18 19:20:16
1    in   2013-12-19 07:51:19   out   2013-12-19 12:00:19  in    2013-12-19 01:00:19    out     2013-12-19 05:30:19     in    2013-12-19 07:51:19   out       2013-12-19 11:00:19

【问题讨论】:

  • 对不起..呵呵..我已经做了一些编辑..提前谢谢你。 :)
  • 这似乎是a direct repeat of this questionthis question。一般来说,最好对现有问题进行编辑,而不是复制它们,以免造成读者重复劳动。

标签: php mysql select row


【解决方案1】:

我认为你会得到最好的,使用纯 SQL 是这样的(未经测试,但你明白了):

SELECT p_id, GROUP_CONCAT(CONCAT(status, " ", timestamp)) inout
FROM TableA
GROUP BY p_id, DATE(timestamp)

【讨论】:

    【解决方案2】:

    我不确切知道如何解决,但我知道您正在寻找的是一个支点: 阅读更多关于: http://en.wikibooks.org/wiki/MySQL/Pivot_table

    【讨论】:

    • 我认为这只是一个左连接.. 但我不知道具体该怎么做。 :) 无论如何感谢您的建议。
    • 问题是特定日期/p_id 组合可能有无限的输入/输出记录,因此查询需要递归。据我所知,MySQL 不会这样做。
    • 你不能使用左连接,因为结果集上的 colluns 是动态的
    • 是否需要更改我的表格结构?请帮忙。
    • 他们昨天在我的第 1 部分问题上给了我这个答案。请看一下..sqlfiddle.com/#!2/6dd1f/8我现在的问题是我想在同一日期每人做3对进出记录(另一个日期的另一行)
    猜你喜欢
    • 2014-01-06
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2022-08-23
    • 1970-01-01
    • 2017-10-19
    相关资源
    最近更新 更多