第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Laravel Eloquent 模型查詢與自定義選擇/加入/訂單

Laravel Eloquent 模型查詢與自定義選擇/加入/訂單

PHP
qq_笑_17 2022-01-14 16:47:41
我有名為 Post 和 PostView 的模型。一個(gè) Post 可以有多個(gè) PostView。我需要獲取按過去 30 天內(nèi)創(chuàng)建的 PostViews 計(jì)數(shù)排序的分頁帖子列表。我可以這樣做withCount:// works but S L O W$posts = Post::withCount(['views' => function($query) {            $query->where('post_views.created_at', '>=', 'DATE_ADD(CURDATE(), INTERVAL -30 DAY)');         }])         ->orderBy('views_count')         ->paginate(10);但是,這會(huì)生成一個(gè)非常慢的查詢,大約需要 24 秒。使用原始 sql 我可以更有效地獲得正確的結(jié)果,如何將其轉(zhuǎn)換為分頁模型集合?這會(huì)生成正確的查詢來獲取前 10 個(gè),但結(jié)果集合是空的。我認(rèn)為這與selectRaw$posts = Post::selectRaw('posts.*, count(post_views.id) as views_count')           ->join('post_views', function($join) {                $join->on('posts.id', '=', 'post_views.post_id')                   ->where('post_views.created_at', '>=', 'DATE_ADD(CURDATE(), INTERVAL -30 DAY)');            })            ->groupBy('posts.id')            ->orderBy('views_count', 'DESC')            ->take(10)            ->get();如果我運(yùn)行直接在 mysql 中生成的查詢,我會(huì)得到結(jié)果:(注意 - 為簡潔起見,截?cái)酁?posts.id)mysql> select posts.id, count(post_views.id) as views_count from `posts` inner join `post_views` on `posts`.`id` = `post_views`.`post_id` and `post_views`.`created_at` >= DATE_ADD(CURDATE(), INTERVAL -30 DAY) group by `posts`.`id` order by `views_count` desc limit 10;+--------+-------------+| id     | views_count |+--------+-------------+| 150277 |          22 ||  43843 |           6 || 138789 |           4 || 180565 |           4 ||  50555 |           3 ||   2679 |           3 || 188572 |           3 || 217454 |           3 || 136736 |           3 || 126472 |           2 |+--------+-------------+10 rows in set (1.26 sec)任何幫助表示贊賞,謝謝。
查看完整描述

3 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

查詢應(yīng)該是:


$posts = Post::selectRaw('posts.*, count(post_views.id) as views_count')

           ->join('post_views', function($join) {

                $join->on('posts.id', '=', 'post_views.post_id')

                   ->where('post_views.created_at', '>=', 'DATE_ADD(CURDATE(), INTERVAL -30 DAY)');

            })

            ->groupBy('posts.id')

            ->orderBy('views_count', 'DESC')

            // ->take(10) // no need of this

            //->get(); // will be using paginate

             ->paginate(10);


查看完整回答
反對(duì) 回復(fù) 2022-01-14
?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

你有沒有嘗試過

PostViews::where('created_at', '>=', 'DATE_ADD(CURDATE(), INTERVAL -30 DAY)')->groupBy('post_id')->selectRaw('count(*) as post_count')->with('post')->paginate(10);

假設(shè)你有一個(gè)索引 created_at


查看完整回答
反對(duì) 回復(fù) 2022-01-14
?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個(gè)贊

我已經(jīng)弄清楚了 - join->where 子句被轉(zhuǎn)義并使查詢無效。不是無效的sql,只是一個(gè)無效的比較,這樣總是沒有結(jié)果。


當(dāng)然,查詢記錄器并沒有顯示它被轉(zhuǎn)義了,所以將查詢復(fù)制到 mysql 中是可行的,這使得追蹤起來比原本應(yīng)該的要困難得多。


這有效:


$posts = Post::select('posts.*', DB::raw('count(post_views.id) as views_count'))

->join('post_views', function($join) {

    $join->on('posts.id', '=', 'post_views.post_id')

        ->where('post_views.created_at', '>=', DB::raw('DATE_ADD(CURDATE(), INTERVAL -30 DAY)'));

    })

->groupBy('posts.id')

->orderBy('views_count', 'DESC')

重要的是:DB::raw('DATE_ADD(CURDATE(), INTERVAL -30 DAY)')


查看完整回答
反對(duì) 回復(fù) 2022-01-14
  • 3 回答
  • 0 關(guān)注
  • 167 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)