我目前有:
$emails = Email::select('username', DB::raw('count(*) as total'))
->groupBy('username')
->get();
返回:
{'username' => 'example', 'total'=>'120' }
我想做的是同时获取某个行值的计数,其中行值等于 1,以获得:
{'username' => 'example', 'total'=>'120', 'sent' => '23' }
喜欢:
DB::raw('count(sentitems) as sent WHERE sentitems = 1')
但当然这样不行。
最佳答案
如果我没理解错的话,你的意思是这样的:
$emails = Email::select('username', DB::raw('count(*) as total'), DB::raw('count(case sentitems when 1 then 1 else null end) as sent')))
->groupBy('username')
->get();
关于php - Laravel Eloquent 计数乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427427/