我不确定time_diff
方法中的内部SQL:
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
除了将此查询作为原始SQL放在DB::query
中之外,还有一种很好的方法吗?
答案 0 :(得分:0)
是的,您可以使用selectRaw()
将子查询添加为字段选择。要创建子查询,可以创建另一个查询构建器。
答案 1 :(得分:0)
试试这个
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");