新聞中心
本文小編為大家詳細(xì)介紹“Laravel中如何用聚合函數(shù)計(jì)算總數(shù)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Laravel中如何用聚合函數(shù)計(jì)算總數(shù)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
創(chuàng)新互聯(lián)公司-專(zhuān)業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性?xún)r(jià)比曲江網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式曲江網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋曲江地區(qū)。費(fèi)用合理售后完善,十年實(shí)體公司更值得信賴(lài)。
假如有電子郵件訂閱服務(wù),希望顯示訂閱者的詳情統(tǒng)計(jì)頁(yè)面如下顯示
訂閱者總數(shù) | 確認(rèn)(confirmed) | 未經(jīng)證實(shí)(unconfirmed) | 取消(cancelled) | 拒絕(bounced) |
---|---|---|---|---|
200 | 150 | 50 | 10 | 5 |
出于本文的目的,假設(shè)我們有一個(gè)subscribers
包含以下格式數(shù)據(jù)的數(shù)據(jù)庫(kù)表:
name | status | |
---|---|---|
小明 | [email protected] | confirmed |
小紅 | [email protected] | unconfirmed |
小軍 | [email protected] | cancelled |
小花 | [email protected] | 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();
上面這樣肯定會(huì)產(chǎn)生五條語(yǔ)句,這樣做肯定是很不好。所以嘗試優(yōu)化一下,會(huì)使用另一個(gè)方法解決執(zhí)行多條語(yǔ)句的問(wèn)題:
$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();
上面先獲取全部訂閱者數(shù)據(jù),然后再對(duì)這個(gè)結(jié)果集進(jìn)行條件統(tǒng)計(jì),使用集合
.模型多條數(shù)據(jù)查詢(xún)返回Illuminate\Database\Eloquent\Collection
。這樣的方法,只適合再數(shù)據(jù)量不大的時(shí)候使用,如果我們的應(yīng)用程序有數(shù)千或數(shù)百萬(wàn)訂閱者,處理的時(shí)間會(huì)很慢,并且會(huì)使用大量?jī)?nèi)存。
條件聚合
實(shí)際上有一種非常簡(jiǎn)單的方法可以查詢(xún)計(jì)算這些總數(shù)。訣竅是將條件放在聚合函數(shù)中。下面是一個(gè) 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
轉(zhuǎn)自鏈接:https://learnku.com/articles/74652
版權(quán)聲明:著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請(qǐng)保留以上作者信息和原文鏈接。
以下是在 Laravel 中使用查詢(xún)構(gòu)建器編寫(xiě)此查詢(xún):
$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();
Boolean 列(字段)
表遷移創(chuàng)建 boolean 字段 , model定義屬于轉(zhuǎn)換 此處不用model為代碼示例,可自行替換為model
如果使用boolean
當(dāng)字段列,將更容易,比如要查詢(xún)subscribers
表中的用戶(hù)是否為擁有不同的角色權(quán)限。假設(shè)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();
這是因?yàn)榫酆虾瘮?shù)count
忽略null
列。與PHP中false || null
返回false
不同,在SQL(以及JavaScript)中,它返回null
。基本上,如果A
可以強(qiáng)制為真,則A || B
返回值A
;否則,返回B
。
這段話(huà)如果沒(méi)理解,就看我下面說(shuō)明:
使用laravel的boolean
列,實(shí)際數(shù)據(jù)表里字段為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中如何用聚合函數(shù)計(jì)算總數(shù)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:Laravel中如何用聚合函數(shù)計(jì)算總數(shù)
文章路徑:http://fisionsoft.com.cn/article/godhsh.html