如何使用Laravel Fluent查询生成器获取行数

时间:2019-06-13 12:42:46

标签: mysql laravel

如何使用Laravel流利的查询生成器获取行数

我附加了用于过滤其他数据的查询,并且还需要获取行数。

这是数据库表:-

enter image description here

  $results = DB::table('newsfeed_posts')
    ->select('newsfeed_posts.*', 'users.first_name as posted_user_first_name', 'users.last_name as posted_user_last_name', 'users.profile_image as posted_user_profile_image','proid.profile_image as posted_receiver_profile_image', 'timeline.first_name as post_receiver_first_name', 'timeline.last_name as post_receiver_last_name', 'timeline.office_branch_id as post_receiver_office_id')
    ->leftJoin('users', 'users.id', 'newsfeed_posts.post_sender_id')
    ->leftJoin('users as timeline', 'timeline.id', 'newsfeed_posts.post_receiver_id') 
    ->leftJoin('users as proid', 'proid.id', 'newsfeed_posts.post_receiver_id') 
    ->where('newsfeed_posts.deleted_status', '0')
    // ->where('newsfeed_posts.post_sender_id', Auth::user()->id)
    ->where('newsfeed_posts.post_receiver_id', Auth::user()->id)
    ->groupBy('newsfeed_posts.id')
    ->orderBy('newsfeed_posts.id', 'DESC')
    ->get();

    return $results;

2 个答案:

答案 0 :(得分:1)

使用纯Laravel查询构建器,您可能必须发出两个单独的查询:

$count = DB::table('newsfeed_posts')
    ->select('newsfeed_posts.*', 'users.first_name as posted_user_first_name', 'users.last_name as posted_user_last_name', 'users.profile_image as posted_user_profile_image','proid.profile_image as posted_receiver_profile_image', 'timeline.first_name as post_receiver_first_name', 'timeline.last_name as post_receiver_last_name', 'timeline.office_branch_id as post_receiver_office_id')
    ->leftJoin('users', 'users.id', 'newsfeed_posts.post_sender_id')
    ->leftJoin('users as timeline', 'timeline.id', 'newsfeed_posts.post_receiver_id') 
    ->leftJoin('users as proid', 'proid.id', 'newsfeed_posts.post_receiver_id') 
    ->where('newsfeed_posts.deleted_status', '0')
    ->where('newsfeed_posts.post_receiver_id', Auth::user()->id)
    ->groupBy('newsfeed_posts.id')
    ->count();

然后,使用当前查询生成所需的结果集。

如果在后台使用MySQL 8+,那么还有另一种选择。我们可以尝试仅使用一个查询就将COUNT()用作分析函数:

$results = DB::table('newsfeed_posts')
    ->selectRaw('COUNT(*) OVER () AS cnt, newsfeed_posts.*', 'users.first_name as posted_user_first_name', 'users.last_name as posted_user_last_name', 'users.profile_image as posted_user_profile_image','proid.profile_image as posted_receiver_profile_image', 'timeline.first_name as post_receiver_first_name', 'timeline.last_name as post_receiver_last_name', 'timeline.office_branch_id as post_receiver_office_id')
    ->leftJoin('users', 'users.id', 'newsfeed_posts.post_sender_id')
    ->leftJoin('users as timeline', 'timeline.id', 'newsfeed_posts.post_receiver_id') 
    ->leftJoin('users as proid', 'proid.id', 'newsfeed_posts.post_receiver_id') 
    ->where('newsfeed_posts.deleted_status', '0')
    ->where('newsfeed_posts.post_receiver_id', Auth::user()->id)
    ->groupBy('newsfeed_posts.id')
    ->orderBy('newsfeed_posts.id', 'DESC')
    ->get();

然后使用别名GROUP BY获得整个结果集(在cnt聚合之后)的计数。

答案 1 :(得分:0)

如果要从数据库中获取所有过滤的记录以及获取的项目数记录。然后,您可以先使用 index price signal stoploss 0 0 1000 True 1.0 1 1 1010 False 990.0 2 2 1020 True 1.0 3 3 1000 False 1010.0 4 4 990 False 1010.0 5 5 980 False 1010.0 6 6 1000 False 1010.0 7 7 1020 True 1.0 8 8 1030 False 1010.0 9 9 1040 False 1010.0 函数来获取所有结果,然后就可以使用get()来获取所有提取的项目计数。

->count()