久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      一文詳解Laravel用聚合函數(shù)計(jì)算總數(shù)(附代碼示例)

      本篇文章給大家?guī)砹岁P(guān)于Laravel的相關(guān)知識(shí),其中主要給大家介紹在Laravel中怎么使用帶有條件聚合函數(shù)來計(jì)算總數(shù)的,下面一起來看一下,希望對(duì)需要的朋友有所幫助。

      一文詳解Laravel用聚合函數(shù)計(jì)算總數(shù)(附代碼示例)

      假如有電子郵件訂閱服務(wù),希望顯示訂閱者的詳情統(tǒng)計(jì)頁面如下顯示

      訂閱者總數(shù) 確認(rèn)(confirmed) 未經(jīng)證實(shí)(unconfirmed) 取消(cancelled) 拒絕(bounced)
      200 150 50 10 5

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

      name email status
      小明 adam@hotmeteor.com confirmed
      小紅 taylor@laravel.com unconfirmed
      小軍 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();
      登錄后復(fù)制

      上面這樣肯定會(huì)產(chǎn)生五條語句,這樣做肯定是很不好。所以嘗試優(yōu)化一下,會(huì)使用另一個(gè)方法解決執(zhí)行多條語句的問題:

      $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();
      登錄后復(fù)制

      上面先獲取全部訂閱者數(shù)據(jù),然后再對(duì)這個(gè)結(jié)果集進(jìn)行條件統(tǒng)計(jì),使用集合.模型多條數(shù)據(jù)查詢返回IlluminateDatabaseEloquentCollection這樣的方法,只適合再數(shù)據(jù)量不大的時(shí)候使用,如果我們的應(yīng)用程序有數(shù)千或數(shù)百萬訂閱者,處理的時(shí)間會(huì)很慢,并且會(huì)使用大量內(nèi)存。

      條件聚合

      實(shí)際上有一種非常簡單的方法可以查詢計(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)保留以上作者信息和原文鏈接。
      登錄后復(fù)制

      以下是在 Laravel 中使用查詢構(gòu)建器編寫此查詢:

      $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>
      登錄后復(fù)制

      Boolean 列(字段)

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

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

      這是因?yàn)榫酆虾瘮?shù)count忽略null列。與PHP中false || null返回false不同,在SQL(以及JavaScript)中,它返回null。基本上,如果A可以強(qiáng)制為真,則A || B返回值A;否則,返回B。

      這段話如果沒理解,就看我下面說明:
      使用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
      登錄后復(fù)制

      翻譯原文:本文只是翻譯一下大概意思,作為自己單純記錄使用

      推薦學(xué)習(xí):《laravel視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)