我正在做一些维护项目,其中所有的都是sql查询,但我想把它转换成laravel。以下是查询:-
$members = Member::select('members.id','members.first_name',
'members.surname','members.username','members.password',
'members.email','user_access.active',
DB::raw('SUM( IF (user_access.active = "y", 1, 0) ) as acount'))
->join('user_access','user_access.member_id','=','members.id')
->where('special_access','=','n')
->groupby('user_access.member_id')
->having('acount','>','0')
->orderby('members.id','desc')
->orderby('members.username','ASC')
->orderby('user_access.note','DESC')
->paginate(30);
当我执行查询时,它给了我错误。在having子句的错误下面
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'acount' in 'having clause' (SQL: select count(*) as aggregate from `members` inner join `user_access` on `user_access`.`member_id` = `members`.`id` where `special_access` = n group by `user_access`.`member_id` having `acount` > 0)
1条答案
按热度按时间gmol16391#
我想这样的事情会对你有帮助的。
或
正如apokryfos在评论中所说:
分页器将尝试获取查询的计数,并在这样做导致此错误时从中删除所有选择。
如果你只需要记录
user_access.active = "y"
然后,您不需要首先选择它们,然后尝试通过HAVING