一文詳解Laravel用聚合函數計算總數(附程式碼範例)
這篇文章為大家帶來了關於Laravel的相關知識,其中主要給大家介紹在Laravel中怎麼使用帶有條件聚合函數來計算總數的,下面一起來看一下,希望對需要的朋友有所幫助。
假如有電子郵件訂閱服務,希望顯示訂閱者的詳情統計頁面如下顯示
訂閱者總數 | 確認(confirmed) | 未經確認(unconfirmed) | 取消(cancelled) | 拒絕(bounced) |
---|---|---|---|---|
#50 | 10 | 5 |
---|---|---|
出於本文的目的,假設我們有一個 | subscribers包含以下格式資料的資料庫表: | |
status | ||
#小明 | adam@hotmeteor.com | confirmed |
小紅 | taylor@laravel.com | unconfirmed |
#小花
adam.wathan@gmail.com
大部分人的做法:
$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_admin、
is_treasurer、
is_editor、
is_manager、欄位
$totals = DB::table('subscribers') ->selectRaw('count(*) as total') ->selectRaw('count(is_admin or null) as admins') ->selectRaw('count(is_treasurer or null) as treasurers') ->selectRaw('count(is_editor or null) as editors') ->selectRaw('count(is_manager or null) as managers') ->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)
,
可以看作表示為(1 or null),這
A為真回傳A,最終sql為
count(is_admin)
//PHP 返回 false var_dump(0 || null) //JavaScript 返回 null console.log(0 || null) //SQL 返回 null SELECT (0 or null) as result
以上是一文詳解Laravel用聚合函數計算總數(附程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel9和CodeIgniter4的最新版本提供了更新的功能和改進。 Laravel9採用MVC架構,提供資料庫遷移、驗證及模板引擎等功能。 CodeIgniter4採用HMVC架構,提供路由、ORM和快取。在性能方面,Laravel9的基於服務提供者設計模式和CodeIgniter4的輕量級框架使其具有出色的性能。在實際應用中,Laravel9適用於需要靈活性和強大功能的複雜項目,而CodeIgniter4適用於快速開發和小型應用程式。

比較Laravel和CodeIgniter的資料處理能力:ORM:Laravel使用EloquentORM,提供類別物件關係映射,而CodeIgniter使用ActiveRecord,將資料庫模型表示為PHP類別的子類別。查詢建構器:Laravel具有靈活的鍊式查詢API,而CodeIgniter的查詢建構器更簡單,基於陣列。資料驗證:Laravel提供了一個Validator類,支援自訂驗證規則,而CodeIgniter的驗證功能內建較少,需要手動編碼自訂規則。實戰案例:用戶註冊範例展示了Lar

對於初學者來說,CodeIgniter的學習曲線更平緩,功能較少,但涵蓋了基本需求。 Laravel提供了更廣泛的功能集,但學習曲線稍陡。在性能方面,Laravel和CodeIgniter都表現出色。 Laravel有更廣泛的文件和活躍的社群支持,而CodeIgniter更簡單、輕量級,具有強大的安全功能。在建立部落格應用程式的實戰案例中,Laravel的EloquentORM簡化了資料操作,而CodeIgniter需要更多的手動配置。

在選擇大型專案框架時,Laravel和CodeIgniter各有優勢。 Laravel針對企業級應用程式而設計,提供模組化設計、相依性注入和強大的功能集。 CodeIgniter是一款輕量級框架,更適合小型到中型項目,強調速度和易用性。對於具有複雜需求和大量用戶的大型項目,Laravel的強大功能和可擴展性更為合適。而對於簡單專案或資源有限的情況下,CodeIgniter的輕量級和快速開發能力則較為理想。

Laravel - Artisan 指令 - Laravel 5.7 提供了處理和測試新指令的新方法。它包括測試 artisan 命令的新功能,下面提到了演示?

對於小型項目,Laravel適用於大型項目,需要強大的功能和安全性。 CodeIgniter適用於非常小的項目,需要輕量級和易用性。

比較了Laravel的Blade和CodeIgniter的Twig模板引擎,根據專案需求和個人偏好進行選擇:Blade基於MVC語法,鼓勵良好程式碼組織和模板繼承。 Twig是第三方函式庫,提供靈活語法、強大過濾器、擴充支援和安全沙箱。

Laravel - Artisan Console - Laravel 框架提供了三種主要的命令列互動工具,分別是:Artisan、Ticker 和 REPL。本章詳細介紹了 Artisan。
