Php 组Concat重复区域的使用
这是我的数据库结构。 表名列表: 我有一个SQL:Php 组Concat重复区域的使用,php,mysql,sql,Php,Mysql,Sql,这是我的数据库结构。 表名列表: 我有一个SQL: select LOPID, EmpId, max(case when LOPStatus = 'Y' then TimeUpdated end) as time_start, max(case when LOPStatus = 'N' then TimeUpdated end) as time_end from MultipleLOPStatus where JobID = 83 group by LOPID
select LOPID, EmpId,
max(case when LOPStatus = 'Y' then TimeUpdated end) as time_start,
max(case when LOPStatus = 'N' then TimeUpdated end) as time_end
from MultipleLOPStatus
where JobID = 83
group by LOPID
结果是
LOPID | EmpId | time_start | time_end
33 | 28 | 2016-03-27T09:33:05-03:00 | 2016-03-27T11:49:17-03:00`
我希望结果是:
LOPID | EmpId | time_start | time_end
33 | 28 | 2016-03-27T09:23:17-03:00 | 2016-03-27T09:31:59-03:00
33 | 28 | 2016-03-27T09:32:30-03:00 | 2016-03-27T11:49:17-03:00
33 | 30 | 2016-03-27T09:33:05-03:00 | 2016-03-27T11:48:53-03:00 `
我们如何做到这一点?如果我理解正确,您只想得到每个相应的开始时间的结束时间。在这种情况下,您不需要使用条件聚合,您可以通过相关查询实现这一点:
SELECT s.LOPID, s.EmpId,s.timeUpdated as time_start,
(SELECT MIN(timeUpdated) FROM MultipleLOPStatus t
WHERE s.LOPID = t.LOPID and t.empid = s.empid and t.LOPStatus = 'N'
AND t.timeUpdated > s.TimeUpdated) as time_end
from MultipleLOPStatus s
where s.JobID = 83
AND s.LOPStatus = 'Y'
为什么要将此结果作为输出?您可以按多个列进行分组。但是从您预期的结果来看,还不清楚哪些列应该在GROUP-BY-list中。在MySQL中进行这种类型的匹配非常困难。这将为相邻的“Y”提供相同的行534。