【问题标题】:Convert selected column values to another row using select with the same id part2 [closed]使用具有相同 id part2 的 select 将选定的列值转换为另一行 [关闭]
【发布时间】:2014-01-06 08:18:04
【问题描述】:

我正在制作一个简单的进出系统。我有 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

同一日期的表格结果一行

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 到表格结果中... :(

标签: php mysql select row


【解决方案1】:

我猜你想要这样的东西:

SELECT a1.id, a.1status, a1.timestamp, a2.status, a2.timetstamp
FROM timetable a1
LEFT JOIN timetable a2 ON a2.id=a1.id AND a2.status = 'out'
WHERE a1.status = 'in'

只有一个输出值的行对我来说没有任何意义。

【讨论】:

  • 让我试试这个......谢谢你。
  • 谢谢你!工作只是一个魅力!
  • 如果我再添加两列状态和时间戳怎么办?先生,我该怎么办?
  • 如果你有第三个身份?添加另一个左连接:LEFT JOIN timetable a3 ON a3.id=a1.id AND a3.status = 'status3' 并相应地添加您的选择列表:SELECT a1.id, a.1status, a1.timestamp, a2.status, a2.timestamp, a3.status, a3.timestamp
  • 不,不是第三种状态。我的意思是另一列用于同一 ID 上的另一组进出记录。 1 在 2013-12-18 16:23:19 出 2013-12-18 17:23:19 在 2013-12-18 18:50:19 出 2013-12-18 18:23:19 在 2013-12- 18 19:23:19 出 2013-12-18 19:23:19
【解决方案2】:

试试这个查询,

select t1.id,t2.status as IN_Status,t2.timestamp as IN_Time,t3.status as OUT_Stats,t3.timestamp as OUT_Time
  from table1 t1 
   left join table1 t2 on (t1.id=t2.id and t2.status ='in')
   left join table1 t3 on (t1.id=t3.id and t3.status ='out') 
   group by t1.id;

查看示例

SQL Fiddle Example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2022-08-23
    • 1970-01-01
    • 2012-02-24
    • 2017-02-14
    • 2012-05-25
    相关资源
    最近更新 更多