[英]Join along with sum and order by in Eloquent
I'm trying to perform the following SQL query using Eloquent: 我正在尝试使用Eloquent执行以下SQL查询:
select s.username, s.link, s.followers, sum(h.count)
from users s, views h where h.username = s.username order by h.timestamp
I'm aware of the join()
, sum()
and select()
methods, however, how can I perform the sum()
and select()
together? 我知道
join()
, sum()
和select()
方法,但是,如何一起执行sum()
和select()
?
To use a SUM()
you will have to use DB::raw
. 要使用
SUM()
,必须使用DB::raw
。 Here is an example that should make your query work: 这是使您的查询正常工作的示例:
DB::table('users as s')
->select(DB::raw('s.username, s.link, s.followers, sum(h.count)'))
->join('views as h', 'h.username', '=', 's.username')
->orderBy('h.timestamp')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.