我有以下查询:
App\User::join('gift_sents', function($builder){
$builder->on('gift_sents.receiver_id', '=', 'users.id');
})
->select('users.*', 'COUNT(gift_sents.receiver_id as total_posts')
->groupBy('gift_sents.id')
->orderBy('total_posts', 'ASC')
->limit(3)->get();
计数不起作用,它应该起作用!
出现以下错误:
未找到列:1054 未知
“字段列表”中的“COUNT(gift_sents.receiver_id”列(SQL:选择
用户。*,
COUNT(gift_sents 。
receiver_idas
total_postsfrom
usersinner join
gift_sentson
gift_sents.
receiver_id=
用户.
idgroup by
gift_sents.
idorder by
total_postsasc limit 3)
最佳答案
我认为应该是:
->select('users.*', DB::raw('COUNT(gift_sents.receiver_id) as total_posts'))
参见文档 here - “原始表达式”部分
关于php - 拉维尔 : How to count in query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38414508/