Laravel:如何计算查询 [英] Laravel : How to count in query
本文介绍了Laravel:如何计算查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下查询:
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();
计数不起作用,应该起作用了!
The count isn't working, Its supposed to be working !
出现以下错误:
未找到列:1054未知栏位[栏位]中的[COUNT(gift_sents.receiver_id)栏(SQL:选择
用户.*,
COUNT(gift_sents .
receiver_id as
receiver_id =
users < id 分组依据
gift_sents .
id 排序依据
total_posts asc限制3)
>
Column not found: 1054 Unknown
column 'COUNT(gift_sents.receiver_id' in 'field list' (SQL: select
users.*,
COUNT(gift_sents.
receiver_idas
total_postsfrom
usersinner join
gift_sentson
gift_sents.
receiver_id=
users.
idgroup by
gift_sents.
idorder by
total_postsasc limit 3)
推荐答案
而不是:
->select('users.*', 'COUNT(gift_sents.receiver_id as total_posts')
您应该使用:
->selectRaw('users.*, COUNT(gift_sents.receiver_id) as total_posts')
这篇关于Laravel:如何计算查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文