Laravel - 在select查询构建器中选择

时间:2016-06-09 09:00:28

标签: mysql laravel query-builder

如何在Laravel Querybuilder中选择所选数据?

我有

DB::table('notifuserdoc')->select('nud_user', 'nud_document', 'name', 'surname', DB::raw('count(nud_id) as count'))
            ->join('user', 'user.id', '=', 'notifuserdoc.nud_user')
            ->groupBy('nud_user', 'nud_document')
            ->having('count', '>', 1)
            ->get();

返回数据如:

enter image description here

但是现在,我想写出用户名和行数:

User 1 - 1
User 2 - 6
User 3 - 3

如何使用“查询”构建器获得此结果?

** 编辑: **

我找到了溶剂:

$sub = DB::table('notifuserdoc')->select('nud_document', 'nud_user', DB::raw('count(nud_id) as dc'))
            ->whereNotNull('nud_user')
            ->groupBy('nud_user', 'nud_document')
            ->having('dc', '>', 1);

$other = DB::table( DB::raw("({$sub->toSql()}) as sub") )->select(
        'nud_user',
        'name', 
        'surname',
        DB::raw('count(dc) as count')
    )
    ->mergeBindings($sub)
    ->join('user', 'user.id', '=', 'sub.nud_user')
    ->groupBy('nud_user')
    ->orderBy('count', 'DESC')
    ->get();

0 个答案:

没有答案