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

<i id='C4Tcb'><tr id='C4Tcb'><dt id='C4Tcb'><q id='C4Tcb'><span id='C4Tcb'><b id='C4Tcb'><form id='C4Tcb'><ins id='C4Tcb'></ins><ul id='C4Tcb'></ul><sub id='C4Tcb'></sub></form><legend id='C4Tcb'></legend><bdo id='C4Tcb'><pre id='C4Tcb'><center id='C4Tcb'></center></pre></bdo></b><th id='C4Tcb'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='C4Tcb'><tfoot id='C4Tcb'></tfoot><dl id='C4Tcb'><fieldset id='C4Tcb'></fieldset></dl></div>

    1. <small id='C4Tcb'></small><noframes id='C4Tcb'>

      <legend id='C4Tcb'><style id='C4Tcb'><dir id='C4Tcb'><q id='C4Tcb'></q></dir></style></legend>
      • <bdo id='C4Tcb'></bdo><ul id='C4Tcb'></ul>

    2. <tfoot id='C4Tcb'></tfoot>

        如何在 Laravel eloquent 中進行子查詢?

        How can I make sub query in laravel eloquent?(如何在 Laravel eloquent 中進行子查詢?)
          <bdo id='HzKYi'></bdo><ul id='HzKYi'></ul>

              <tbody id='HzKYi'></tbody>

            <small id='HzKYi'></small><noframes id='HzKYi'>

              • <i id='HzKYi'><tr id='HzKYi'><dt id='HzKYi'><q id='HzKYi'><span id='HzKYi'><b id='HzKYi'><form id='HzKYi'><ins id='HzKYi'></ins><ul id='HzKYi'></ul><sub id='HzKYi'></sub></form><legend id='HzKYi'></legend><bdo id='HzKYi'><pre id='HzKYi'><center id='HzKYi'></center></pre></bdo></b><th id='HzKYi'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='HzKYi'><tfoot id='HzKYi'></tfoot><dl id='HzKYi'><fieldset id='HzKYi'></fieldset></dl></div>

              • <legend id='HzKYi'><style id='HzKYi'><dir id='HzKYi'><q id='HzKYi'></q></dir></style></legend>
                  <tfoot id='HzKYi'></tfoot>

                  本文介紹了如何在 Laravel eloquent 中進行子查詢?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  當我使用 db raw 時,它可以工作

                  When I use db raw, it works

                  我的查詢使用的是這樣的 db raw :

                  My query is using db raw like this :

                  $products = DB::select(DB::raw('SELECT * 
                                  FROM (
                                      SELECT a.*, b.name AS store_name, b.address
                                      FROM products a
                                      JOIN stores b ON b.id = a.store_id
                                      WHERE a.category_id = '.$category_id.'
                                      ORDER BY a.total_sold DESC, a.updated_at DESC
                                      LIMIT '.$num.'
                                  ) AS product
                                  GROUP BY store_id'));
                  

                  它有效.但是我想改用laravel eloquent

                  It works. But I want to change it use laravel eloquent

                  我是這樣嘗試的:

                  $products = Product::where('category_id', '=', $category_id)
                       ->with('store')
                       ->groupBy('store_id')
                       ->orderBy('total_sold','desc')
                       ->orderBy('updated_at', 'desc')
                       ->take($num)
                       ->get();
                  

                  它也有效.但是 orderBy updated_at 沒有執行

                  It also works. But orderBy updated_at not executed

                  我該如何解決?

                  推薦答案

                  在我看來,您使用的 group by 不正確.即使您在查詢之前檢索到了正確的結果,我也認為這是偶然的.Group by 應該用于聚合查詢結果并獲取聚合列值.選擇實際上未聚合的列如果使用不當可能會很危險.

                  It seems to me that you are using group by incorrectly. Even if you retrieved correct results for the query before it looks to me that it was by chance anyway. Group by should be used to aggregate query results and get aggregated column values. By choosing columns which are not actually aggregated can be dangerous if used incorrectly.

                  來自 5.6 版的 Mysql 文檔:

                  From the Mysql docs for version 5.6:

                  MySQL 擴展了 GROUP BY 的標準 SQL 使用,以便選擇列表可以引用未在 GROUP BY 子句中命名的非聚合列.這意味著前面的查詢在 MySQL 中是合法的.您可以使用此功能通過避免不必要的列排序和分組來獲得更好的性能.但是,這主要在未在 GROUP BY 中命名的每個非聚合列中的所有值對于每個組都相同時很有用.服務器可以自由地從每個組中選擇任何值,因此除非它們相同,否則選擇的值是不確定的.此外,添加 ORDER BY 子句不會影響從每個組中選擇值.結果集排序發生在選擇值之后,ORDER BY 不影響服務器選擇每個組中的哪些值.

                  MySQL extends the standard SQL use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Result set sorting occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses.

                  此外,從 MySql 5.7.5 開始,默認 SQL 模式包括 ONLY_FULL_GROUP_BY 標志,它將:

                  Additionally as of MySql 5.7.5 the default SQL mode includes ONLY_FULL_GROUP_BY flag which will:

                  拒絕選擇列表、HAVING 條件或 ORDER BY 列表引用非聚合列的查詢,這些列既不在 GROUP BY 子句中命名,也不在功能上依賴于(唯一確定的)GROUP BY 列.

                  Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.

                  出于教育目的,您應該能夠像這樣使用 Laravel 實現完全相同的查詢(未經測試且不使用表別名),但我會避免使用它:

                  For educational purposes you should be able to achieve the exact same query with Laravel like this (untested and without the use of table aliases), but I would avoid using it:

                  $subQuery = Products::selectRaw('products.*, stores.name as store_name, stores.address')
                      ->join('stores', 'stores.id', '=', 'products.store_id')
                      ->where('products.category_id', '=', $category_id)
                      ->orderBy('products.total_sold', 'DESC')
                      ->orderBy('products.updated_at', 'DESC')
                      ->take($num)
                  
                  $products = DB::table(DB::raw('(' . $subQuery->toSql() . ') t'))
                      ->groupBy('store_id')
                      ->setBindings($subQuery->getBindings())
                      ->get();
                  

                  但在我看來,您想要做的似乎是將所有商店與您想要的類別中的產品放在一起.所以最 Laravel 的解決方案可能是這樣的:

                  But to me it seems that what you're trying to do is get all the stores together with products in your desired category. So the most Laravel solution would probably be something like:

                  Stores::with(['products' => function($productsQuery) use ($category_id) {
                      // This limits all the retrieved products to the provided category
                      $productsQuery
                          ->where('category_id', '=', $category_id)
                          ->orderBy('total_sold', 'DESC')
                          ->orderBy('updated_at', 'DESC');
                  }])->whereHas('products', function($productsQuery) use ($category_id) {
                      // This makes sure that the store actually has at least one product from the category
                      $productsQuery->where('category_id', '=', $category_id);
                  })->get();
                  

                  通過查看您的查詢,我可能做出了錯誤的假設,但目前沒有多大意義......無論如何我都會從那里開始.

                  I might have made wrong assumptions by looking at your query but it doesn't make much sense at the moment... I would start from there anyway.

                  這篇關于如何在 Laravel eloquent 中進行子查詢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)

                    1. <tfoot id='VTpaa'></tfoot>
                      <i id='VTpaa'><tr id='VTpaa'><dt id='VTpaa'><q id='VTpaa'><span id='VTpaa'><b id='VTpaa'><form id='VTpaa'><ins id='VTpaa'></ins><ul id='VTpaa'></ul><sub id='VTpaa'></sub></form><legend id='VTpaa'></legend><bdo id='VTpaa'><pre id='VTpaa'><center id='VTpaa'></center></pre></bdo></b><th id='VTpaa'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='VTpaa'><tfoot id='VTpaa'></tfoot><dl id='VTpaa'><fieldset id='VTpaa'></fieldset></dl></div>

                    2. <legend id='VTpaa'><style id='VTpaa'><dir id='VTpaa'><q id='VTpaa'></q></dir></style></legend>
                        <tbody id='VTpaa'></tbody>

                        <small id='VTpaa'></small><noframes id='VTpaa'>

                          • <bdo id='VTpaa'></bdo><ul id='VTpaa'></ul>
                          • 主站蜘蛛池模板: 超碰导航| 亚洲一区二区欧美 | 在线免费看黄 | 午夜精品久久久久久不卡欧美一级 | 久久一级 | 在线免费观看黄视频 | 成人h动漫精品一区二区器材 | 美女视频一区二区三区 | 日韩喷潮 | 日韩精品免费 | 中文字幕av免费 | 国产一级精品毛片 | 不卡视频一区二区三区 | 日韩伦理一区二区 | 羞羞视频免费观 | 成人无遮挡毛片免费看 | 久视频在线 | 久久久久久久一级 | 在线亚洲人成电影网站色www | 久久免费国产视频 | 国产乱码精品1区2区3区 | 日日操日日舔 | 欧美视频区 | 欧美精品一区二区在线观看 | 国产激情视频 | 久久久做 | 国产一区二区精华 | 国产亚洲第一页 | 网站黄色av | 日韩在线一区二区 | 色妹子综合网 | 国产网站在线播放 | 免费观看www | 久久99网| 精品免费国产一区二区三区四区 | 一区二区三区播放 | 在线观看av网站永久 | 国产精品揄拍一区二区 | 午夜理伦三级理论三级在线观看 | 玩丰满女领导对白露脸hd | 男人天堂网址 |