在findercakephp 3.5.10中帮助编写和执行此查询
之后,我需要按user_id对结果进行分组。
为了便于管理员使用,下面是表结构。
select user_id, GRP, MIN(time) as Start, MAX(time) as End
from (select user_id, time,
@grp:=@grp+if(time>@prev+interval 30 minute,1,0) as GRP,
@prev:=time
from user_log, (select @prev:=NULL, @grp:=1) X
order by time) A
group by GRP
详细信息:
答案 0 :(得分:-3)
$conn = ConnectionManager::get('default');
$sql = 'select user_id, GRP, MIN(time) as Start, MAX(time) as End from (select user_id, time, @grp:=@grp+if(time>@prev+interval 30 minute,1,0) as GRP,@prev:=time from user_log, (select @prev:=NULL, @grp:=1) X order by time) A group by GRP';
$stmt = $conn->execute($sql);
$user = $stmt->fetchAll('assoc');