久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 在單個子句中使用多個 where 和 sum

Laravel use multiple where and sum in single clause(Laravel 在單個子句中使用多個 where 和 sum)
本文介紹了Laravel 在單個子句中使用多個 where 和 sum的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在我的數據庫中,我有 instagram_actions_histories 表,其中有 action_type 列,在該列中我有不同的數據,例如 123

in my database i have instagram_actions_histories table which into that i have action_type column, in the column i have unlike data such as 1 or 2 or 3

我正在嘗試獲取關系船中的此表數據并對存儲在列中的這些值求和,例如

i'm trying to get this table data in relation ship and summing this values which stored in column, for example

$userAddedPagesList = auth()->user()->instagramPages()->with([
        'history' => function ($query)  {
            $query->select(['action_type as count'])->whereActionType(1)->sum('action_type');
        }
    ]
)->get();

順便說一句,這段代碼是不正確的,因為我想得到所有 history 里面有多個 sum

btw, this code is not correct, in that i want to get all history with multiple sum inside that

whereActionType(1)->sum('action_type')
whereActionType(2)->sum('action_type')
whereActionType(3)->sum('action_type')

例如(偽代碼):

$userAddedPagesList = auth()->user()->instagramPages()->with([
        'history' => function ($query)  {
            $query->select(['action_type as like'])->whereActionType(1)->sum('action_type');
            $query->select(['action_type as follow'])->whereActionType(2)->sum('action_type');
            $query->select(['action_type as unfollow'])->whereActionType(3)->sum('action_type');
        }
    ]
)->get();

更新:

$userAddedPagesList = auth()->user()->instagramPages()->with([
        'history' => function ($query) {
            $query->select('*')
                ->selectSub(function ($query) {
                    return $query->selectRaw('SUM(action_type)')
                        ->where('action_type', '=', '1');
                }, 'like')
                ->selectSub(function ($query) {
                    return $query->selectRaw('SUM(action_type)')
                        ->where('action_type', '=', '2');
                }, 'follow')
                ->selectSub(function ($query) {
                    return $query->selectRaw('SUM(action_type)')
                        ->where('action_type', '=', '3');
                }, 'followBack');
        }
    ]
)->get();

錯誤:

Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select *, (select SUM(action_type) where `action_type` = 1) as `like`, (select SUM(action_type) where `action_type` = 2) as `follow`, (select SUM(action_type) where `action_type` = 3) as `followBack` from `instagram_actions_histories` where `instagram_actions_histories`.`account_id` in (1, 2, 3))

我該如何實施這個解決方案?

how can i implementing this solution?

更新:

InstagramAccount 類:

class InstagramAccount extends Model
{
    ...

    public function schedule()
    {
        return $this->hasOne(ScheduleInstagramAccounts::class, 'account_id');
    }

    public function history()
    {
        return $this->hasMany(InstagramActionsHistory::class, 'account_id');
    }
}

InstagramActionsHistory 類:

class InstagramActionsHistory extends Model
{
    protected $guarded=['id'];

    public function page(){
        return $this->belongsTo(InstagramAccount::class);
    }
}

用戶類別:

class User extends Authenticatable
{
    use Notifiable;

    ...

    public function instagramPages()
    {
        return $this->hasMany(InstagramAccount::class);
    }
}

推薦答案

另一種為不同類型的動作獲取條件總和的方法,你可以在你的 中定義一個 hasOne() 關系InstagramAccount 模型如

Another approach to get conditional sum for your different types of action, you can define a hasOne() relation in your InstagramAccount model like

public function history_sum()
{
    return $this->hasOne(InstagramActionsHistory::class, 'account_id')
        ->select('account_id',
            DB::raw('sum(case when action_type = 1 then 0 END) as `like`'),
            DB::raw('sum(case when action_type = 2 then 0 END) as `follow`'),
            DB::raw('sum(case when action_type = 3 then 0 END) as `followBack`')
        )->groupBy('account_id');
}

然后您可以將相關數據預先加載為

Then you can eager load the related data as

$userAddedPagesList = auth()->user()->instagramPages()->with('history_sum')->get();

采用這種方法將僅執行一個額外的查詢,以根據您的條件獲得 3 個不同的總和結果

Going through with this approach will execute only one extra query to get 3 different sum results based on your criteria

select `account_id`,
sum(case when action_type = 1 then action_type else 0 END) as `like`, 
sum(case when action_type = 2 then action_type else 0 END) as `follow`, 
sum(case when action_type = 3 then action_type else 0 END) as `followBack` 
from `instagram_actions_histories` 
where `instagram_actions_histories`.`account_id` in (?, ?, ?) 
group by `account_id`

雖然與使用 withCount 的其他方法(這也是一個有效的答案)相比,將為每個操作類型添加 3 個相關的相關子查詢,這可能會導致性能開銷,生成的查詢將看起來有些東西如下圖

While as compare to other approach (which is a valid answer also) using withCount will add 3 dependent correlated sub queries for each action type which may result as a performance overhead, generated query will look something like below

select `instagram_account`.*, 
(select sum(action_type) from `instagram_actions_histories` where `instagram_account`.`id` = `instagram_actions_histories`.`account_id` and `action_type` = ?) as `like`, 
(select sum(action_type) from `instagram_actions_histories` where `instagram_account`.`id` = `instagram_actions_histories`.`account_id` and `action_type` = ?) as `follow`,
(select sum(action_type) from `instagram_actions_histories` where `instagram_account`.`id` = `instagram_actions_histories`.`account_id` and `action_type` = ?) as `followBack`
from `instagram_account` 
where `instagram_account`.`user_id` = ? 
and `instagram_account`.`user_id` is not null

要檢查生成的查詢,請參閱Laravel 5.3 - 如何記錄頁面上的所有查詢?

To check the generated queries refer to Laravel 5.3 - How to log all queries on a page?

這篇關于Laravel 在單個子句中使用多個 where 和 sum的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數據))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 99视频入口| 欧美成人精品一区二区三区 | 国产日韩精品在线 | 精品亚洲一区二区三区 | 国产日韩精品一区二区 | 国产精品永久在线观看 | 一区二区国产精品 | 欧美综合久久 | 欧美日韩在线一区二区三区 | 成人在线视频免费看 | 日韩福利 | 91精品国产综合久久婷婷香蕉 | 91精品国产91久久久久久密臀 | 久草视频在线播放 | 午夜成人免费视频 | 国产在线精品一区 | 亚洲精品一区二三区不卡 | 国产亚洲一级 | 精品人伦一区二区三区蜜桃网站 | 日本国产一区二区 | 毛色毛片免费看 | 国产999精品久久久 日本视频一区二区三区 | 日韩精品视频在线观看一区二区三区 | 日本在线免费视频 | 精品国产91 | 色偷偷噜噜噜亚洲男人 | 韩三级在线观看 | 国产视频一区在线 | 午夜男人的天堂 | 精品美女视频在线观看免费软件 | 91亚洲免费 | 中文字幕免费视频 | 欧美乱大交xxxxx另类电影 | 日本黄色的视频 | 欧美日韩成人影院 | 国产资源视频 | 久久国产精品视频观看 | 高清久久久 | 国内精品久久精品 | 色综合天天天天做夜夜夜夜做 | 人人射人人 |