laravel count的使用


rt

學習源頭:

https://blog.csdn.net/chajinglong/article/details/51954010

四、聚合

查詢構建器還提供了各種聚合方法,如統計,馬克斯,min,avg和總和。

Using Aggregate Methods

復制代碼代碼如下:

$users = DB::table('users')->count();
$price = DB::table('orders')->max('price');
$price = DB::table('orders')->min('price');
$price = DB::table('orders')->avg('price');
$total = DB::table('users')->sum('votes');

 

Raw Expressions

有時您可能需要使用一個原始表達式的查詢。這些表達式將注入的查詢字符串,所以小心不要創建任何SQL注入點!創建一個原始表達式,可以使用DB:rawmethod:

Using A Raw Expression

 

復制代碼代碼如下:


$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM