gol*_*ife 4 php mysql laravel eloquent cartalyst-sentry
我有一个users表,其中有一个名为 的整数字段ep。
现在,我想检索按 EP 字段排序的特定组中的所有用户,并使用生成的名为 的 MySQL 字段rank。
这是我尝试过的:
DB::transaction(function()
{
DB::statement("SET @rank:=0");
$group = Sentry::findGroupByName('Player');
$users = Sentry::getUserProvider()->createModel()
->join('throttle', 'throttle.user_id', '=', 'users.id')
->join('users_groups', 'users_groups.user_id', '=', 'users.id')
->where('throttle.banned', 0)
->where('throttle.suspended', 0)
->where('users_groups.group_id', $group->id)
->orderBy('ep', 'desc')
->paginate(100, array('@rank := @rank + 1', 'users.ep', 'users.username'));
});
Run Code Online (Sandbox Code Playgroud)
但我收到了这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '@rank := @rank + 1' in 'field list' (SQL: select `@rank := @rank + 1`, `users`.`ep`, `users`.`username` from `users` inner join `throttle` on `throttle`.`user_id` = `users`.`id` inner join `users_groups` on `users_groups`.`user_id` = `users`.`id` where `throttle`.`banned` = 0 and `throttle`.`suspended` = 0 and `users_groups`.`group_id` = 2 order by `ep` desc limit 100 offset 0)
Run Code Online (Sandbox Code Playgroud)
遇到同样的问题并在以下位置找到了正确的解决方案:https ://laravel.io/forum/12-18-2014-how-to-initialize-user-define-mysql-variables
DB::statement( DB::raw( 'SET @total := 0'));
$results = DB::select( $query); // only the "select.." part here
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8665 次 |
| 最近记录: |