I am not sure about the inner SQL inside time_diff
method:
SELECT t1.started_at as chain_break,
time_to_sec(timediff(t1.started_at,IFNULL(
(SELECT MAX(t2.ended_at)
FROM status_records t2
WHERE t2.user_id=189
AND t2.started_at< t1.started_at
), t1.started_at
)
)
) / 3600 AS time_off
FROM status_records t1
WHERE t1.user_id=189
ORDER BY t1.ended_at DESC
LIMIT 6
Is there a nice way to do it except putting this query in DB::query
as raw SQL?
Yes, there is, you can use selectRaw()
to add your subquery as a field selection. To create the subquery, you can create another query builder.
Try This
DB::raw("SELECT t1.started_at as chain_break,
time_to_sec(timediff(t1.started_at,IFNULL(
(SELECT MAX(t2.ended_at)
FROM status_records t2
WHERE t2.user_id=189
AND t2.started_at< t1.started_at
), t1.started_at
)
)
) / 3600 AS time_off
FROM status_records t1
WHERE t1.user_id=189
ORDER BY t1.ended_at DESC
LIMIT 6");
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.