I'm trying to perform the following SQL query using Eloquent:
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?
To use a SUM()
you will have to use 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();
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.