简体   繁体   中英

Cakephp make a request in the finder

Help write and execute this query in finder cakephp 3.5.10

After, I need to group the result by user_id.

Below is the table structure, for the convenience of the from Adminer.

    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

Details:

数据

$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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM