[英]How to translate this query to Laravel Query Builder?
I am not sure about the inner SQL inside time_diff
method: 我不确定
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
Is there a nice way to do it except putting this query in DB::query
as raw SQL? 除了将此
DB::query
作为原始SQL放入DB::query
之外,还有什么好方法吗?
Yes, there is, you can use selectRaw()
to add your subquery as a field selection. 是的,您可以使用
selectRaw()
将子查询添加为字段选择。 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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.