最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Laravel中如何用聚合函數(shù)計(jì)算總數(shù)

本文小編為大家詳細(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)
20015050105

出于本文的目的,假設(shè)我們有一個(gè)subscribers包含以下格式數(shù)據(jù)的數(shù)據(jù)庫(kù)表:

nameemailstatus
小明[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();

Total: {{ $totals->total }}
Confirmed: {{ $totals->confirmed }}
Unconfirmed: {{ $totals->unconfirmed }}
Cancelled: {{ $totals->cancelled }}
Bounced: {{ $totals->bounced }}

Boolean 列(字段)

表遷移創(chuàng)建 boolean 字段 , model定義屬于轉(zhuǎn)換 此處不用model為代碼示例,可自行替換為model

如果使用boolean當(dāng)字段列,將更容易,比如要查詢(xún)subscribers表中的用戶(hù)是否為擁有不同的角色權(quán)限。假設(shè)subscribers表中有is_admin、is_treasureris_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