我有以下查询:
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();
计数不起作用,它应该正常工作!
出现以下错误:
Column not found: 1054 Unknown
column 'COUNT(gift_sents.receiver_id' in 'field list' (SQL: select
用户.*,
COUNT(gift_sents .
receiver_id as
TOTAL_POSTS from
用户inner join
gift_sents on
gift_sents {{1 }} {receiver_id {1}} {用户{1}} ID为.
{gift_sents {1}} ID为=
{TOTAL_POSTS {1}}
答案 0 :(得分:3)
我认为应该是:
->select('users.*', DB::raw('COUNT(gift_sents.receiver_id) as total_posts'))
请参阅文档here - '原始表达式'节
答案 1 :(得分:0)
而不是:
->select('users.*', 'COUNT(gift_sents.receiver_id as total_posts')
你应该使用:
->selectRaw('users.*, COUNT(gift_sents.receiver_id) as total_posts')