pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<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="hnbdnvv" 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 中進(jìn)行子查詢?

        How can I make sub query in laravel eloquent?(如何在 Laravel eloquent 中進(jìn)行子查詢?)
          <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="vbdlv9b" 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 中進(jìn)行子查詢?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問題描述

                  當(dāng)我使用 db raw 時(shí),它可以工作

                  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 沒有執(zhí)行

                  It also works. But orderBy updated_at not executed

                  我該如何解決?

                  推薦答案

                  在我看來(lái),您使用的 group by 不正確.即使您在查詢之前檢索到了正確的結(jié)果,我也認(rèn)為這是偶然的.Group by 應(yīng)該用于聚合查詢結(jié)果并獲取聚合列值.選擇實(shí)際上未聚合的列如果使用不當(dāng)可能會(huì)很危險(xiǎn).

                  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.

                  來(lái)自 5.6 版的 Mysql 文檔:

                  From the Mysql docs for version 5.6:

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

                  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 開始,默認(rèn) SQL 模式包括 ONLY_FULL_GROUP_BY 標(biāo)志,它將:

                  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.

                  出于教育目的,您應(yīng)該能夠像這樣使用 Laravel 實(shí)現(xiàn)完全相同的查詢(未經(jīng)測(cè)試且不使用表別名),但我會(huì)避免使用它:

                  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();
                  

                  但在我看來(lái),您想要做的似乎是將所有商店與您想要的類別中的產(chǎn)品放在一起.所以最 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();
                  

                  通過查看您的查詢,我可能做出了錯(cuò)誤的假設(shè),但目前沒有多大意義......無(wú)論如何我都會(huì)從那里開始.

                  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.

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

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

                  相關(guān)文檔推薦

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

                    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="dv7frnd" 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>
                          • 主站蜘蛛池模板: 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 章丘丰源机械有限公司 - 三叶罗茨风机,罗茨鼓风机,罗茨风机 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 螺纹三通快插接头-弯通快插接头-宁波舜驰气动科技有限公司 | 垃圾压缩设备_垃圾处理设备_智能移动式垃圾压缩设备--山东明莱环保设备有限公司 | 鄂泉泵业官网|(杭州、上海、全国畅销)大流量防汛排涝泵-LW立式排污泵 | 杜甫仪器官网|实验室平行反应器|升降水浴锅|台式低温循环泵 | [官网]叛逆孩子管教_戒网瘾学校_全封闭问题青少年素质教育_新起点青少年特训学校 | 换网器_自动换网器_液压换网器--郑州海科熔体泵有限公司 | 南京兰江泵业有限公司-水解酸化池潜水搅拌机-絮凝反应池搅拌机-好氧区潜水推进器 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | 广东机电安装工程_中央空调工程_东莞装饰装修-广东粤标建设有限公司 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 宏源科技-房地产售楼系统|线上开盘系统|售楼管理系统|线上开盘软件 | 对夹式止回阀厂家,温州对夹式止回阀制造商--永嘉县润丰阀门有限公司 | 隧道烘箱_隧道烘箱生产厂家-上海冠顶专业生产烘道设备 | 合肥网带炉_安徽箱式炉_钟罩炉-合肥品炙装备科技有限公司 | 压片机_高速_单冲_双层_花篮式_多功能旋转压片机-上海天九压片机厂家 | 胶水,胶粘剂,AB胶,环氧胶,UV胶水,高温胶,快干胶,密封胶,结构胶,电子胶,厌氧胶,高温胶水,电子胶水-东莞聚力-聚厉胶粘 | TTCMS自助建站_网站建设_自助建站_免费网站_免费建站_天天向上旗下品牌 | 卸料器-卸灰阀-卸料阀-瑞安市天蓝环保设备有限公司 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 庭院灯_太阳能景观灯_草坪灯厂家_仿古壁灯-重庆恒投科技 | 泥沙分离_泥沙分离设备_泥砂分离机_洛阳隆中重工机械有限公司 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 拉力机-拉力试验机-万能试验机-电子拉力机-拉伸试验机-剥离强度试验机-苏州皖仪实验仪器有限公司 | 丙烷/液氧/液氮气化器,丙烷/液氧/液氮汽化器-无锡舍勒能源科技有限公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 艺术涂料|木纹漆施工|稻草漆厂家|马来漆|石桦奴|水泥漆|选加河南天工涂料 | 悬浮拼装地板_篮球场木地板翻新_运动木地板价格-上海越禾运动地板厂家 | 茶楼装修设计_茶馆室内设计效果图_云臻轩茶楼装饰公司 | 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | 校园气象站_超声波气象站_农业气象站_雨量监测站_风途科技 | 泉州陶瓷pc砖_园林景观砖厂家_石英砖地铺石价格 _福建暴风石英砖 | 上海电子秤厂家,电子秤厂家价格,上海吊秤厂家,吊秤供应价格-上海佳宜电子科技有限公司 | 环球电气之家-中国专业电气电子产品行业服务网站! | 苏商学院官网 - 江苏地区唯一一家企业家自办的前瞻型、实操型商学院 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 |