首頁  >  文章  >  php框架  >  一文詳解Laravel用聚合函數計算總數(附程式碼範例)

一文詳解Laravel用聚合函數計算總數(附程式碼範例)

藏色散人
藏色散人轉載
2023-01-20 16:08:571151瀏覽

這篇文章為大家帶來了關於Laravel的相關知識,其中主要給大家介紹在Laravel中怎麼使用帶有條件聚合函數來計算總數的,下面一起來看一下,希望對需要的朋友有所幫助。

一文詳解Laravel用聚合函數計算總數(附程式碼範例)

假如有電子郵件訂閱服務,希望顯示訂閱者的詳情統計頁面如下顯示

訂閱者總數 確認(confirmed) 未經確認(unconfirmed) 取消(cancelled) 拒絕(bounced)

##200150subscribers##nameemailstatus#小明adam@hotmeteor.comconfirmed小紅taylor@laravel.comunconfirmed
#50 10 5
出於本文的目的,假設我們有一個包含以下格式資料的資料庫表:
小軍

jonathan@reinink.ca

cancelled

#小花adam.wathan@gmail.com

#bounced

大部分人的做法:

$total = Subscriber::count();
$confirmed = Subscriber::where('status', 'confirmed')->count();
$unconfirmed = Subscriber::where('status', 'unconfirmed')->count();
$cancelled = Subscriber::where('status', 'cancelled')->count();
$bounced = Subscriber::where('status', 'bounced')->count();

上面這樣肯定會產生五條語句,這樣做肯定是很不好。所以試著優化一下,會用另一個方法來解決執行多條語句的問題:

$subscribers = Subscriber::all();
$total = $subscribers->count();
$confirmed = $subscribers->where('status', 'confirmed')->count();
$unconfirmed = $subscribers->where('status', 'unconfirmed')->count();
$cancelled = $subscribers->where('status', 'cancelled')->count();
$bounced = $subscribers->where('status', 'bounced')->count();
上面先取得全部訂閱者數據,然後再對這個結果集進行條件統計,使用集合

.模型多條資料查詢傳回

Illuminate\Database\Eloquent\Collection這樣的方法,只適合再數據量不大的時候使用,如果我們的應用程式有數千或數百萬訂閱者,處理的時間會很慢,並且會使用大量內存。

條件聚合其實有一個非常簡單的方法可以查詢計算這些總數。訣竅是將條件放在聚合函數中。以下是一個SQL 範例:

select
  count(*) as total,
  count(case when status = 'confirmed' then 1 end) as confirmed,
  count(case when status = 'unconfirmed' then 1 end) as unconfirmed,
  count(case when status = 'cancelled' then 1 end) as cancelled,
  count(case when status = 'bounced' then 1 end) as bounced
from subscribers

 total | confirmed | unconfirmed | cancelled | bounced
-------+-----------+-------------+-----------+---------
   200 |       150 |          50 |        30 |      25

————————————————
原文作者:4pmzzzzzzzzzz
转自链接:https://learnku.com/articles/74652
版权声明:著作权归作者所有。商业转载请联系作者获得授权,非商业转载请保留以上作者信息和原文链接。
以下是在Laravel 中使用查詢建構器編寫此查詢:
$totals = DB::table('subscribers')
    ->selectRaw('count(*) as total')
    ->selectRaw("count(case when status = 'confirmed' then 1 end) as confirmed")
    ->selectRaw("count(case when status = 'unconfirmed' then 1 end) as unconfirmed")
    ->selectRaw("count(case when status = 'cancelled' then 1 end) as cancelled")
    ->selectRaw("count(case when status = 'bounced' then 1 end) as bounced")
    ->first();

<div>Total: {{ $totals->total }}</div>
<div>Confirmed: {{ $totals->confirmed }}</div>
<div>Unconfirmed: {{ $totals->unconfirmed }}</div>
<div>Cancelled: {{ $totals->cancelled }}</div>
<div>Bounced: {{ $totals->bounced }}</div>
Boolean 欄位(欄位)#表遷移建立boolean 欄位, model定義屬於轉換

此處不用model為程式碼範例,可自行替換為model

如果使用boolean#當欄位列,將會更容易,例如要查詢subscribers表中的使用者是否為擁有不同的角色權限。假設subscribers表格中有is_adminis_treasureris_editoris_manager、欄位

$totals = DB::table(&#39;subscribers&#39;)
    ->selectRaw(&#39;count(*) as total&#39;)
    ->selectRaw(&#39;count(is_admin or null) as admins&#39;)
    ->selectRaw(&#39;count(is_treasurer or null) as treasurers&#39;)
    ->selectRaw(&#39;count(is_editor or null) as editors&#39;)
    ->selectRaw(&#39;count(is_manager or null) as managers&#39;)
    ->first();
這是因為聚合函數count

忽略

null
列。與PHP中false || null回傳false不同,在SQL(以及JavaScript)中,它會傳回null。基本上,如果A可以強制為真,則
A || B傳回值A;否則,傳回B這段話如果沒理解,就看我下面說明:使用laravel的boolean列,實際資料表裡欄位為tinyint,值為0(false)
1(true), 例如小明的is_admin欄位為1(true),

count(is_admin or null)

可以看作表示為(1 or null),這

A

為真回傳A,最終sql為
count(is_admin)

。 ###反之則是如###is_admin###字段為###0(false)###,最終sql為###count(null)###,則忽略此列###
//PHP  返回 false
var_dump(0 || null) 

//JavaScript 返回 null
console.log(0 || null)

//SQL 返回 null
SELECT (0 or null) as result
# #####翻譯原文###:本文只是翻譯一下大概意思,作為自己單純記錄使用######推薦學習:《###laravel影片教學###》######

以上是一文詳解Laravel用聚合函數計算總數(附程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除