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

<legend id='tPY04'><style id='tPY04'><dir id='tPY04'><q id='tPY04'></q></dir></style></legend>

          <bdo id='tPY04'></bdo><ul id='tPY04'></ul>

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

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

        如何使用 Laravel Eloquent 創(chuàng)建子查詢?

        How to create a subquery using Laravel Eloquent?(如何使用 Laravel Eloquent 創(chuàng)建子查詢?)
      2. <i id='DtUjx'><tr id='DtUjx'><dt id='DtUjx'><q id='DtUjx'><span id='DtUjx'><b id='DtUjx'><form id='DtUjx'><ins id='DtUjx'></ins><ul id='DtUjx'></ul><sub id='DtUjx'></sub></form><legend id='DtUjx'></legend><bdo id='DtUjx'><pre id='DtUjx'><center id='DtUjx'></center></pre></bdo></b><th id='DtUjx'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='DtUjx'><tfoot id='DtUjx'></tfoot><dl id='DtUjx'><fieldset id='DtUjx'></fieldset></dl></div>
        <legend id='DtUjx'><style id='DtUjx'><dir id='DtUjx'><q id='DtUjx'></q></dir></style></legend>

            • <small id='DtUjx'></small><noframes id='DtUjx'>

            • <tfoot id='DtUjx'></tfoot>
                <bdo id='DtUjx'></bdo><ul id='DtUjx'></ul>

                  <tbody id='DtUjx'></tbody>
                  本文介紹了如何使用 Laravel Eloquent 創(chuàng)建子查詢?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有以下 Eloquent 查詢(這是一個查詢的簡化版本,它由更多的 whereorWhere 組成,因此明顯的迂回方式這 - 理論很重要):

                  I have the following Eloquent query (This is a simplified version of a query which consists of of more wheres and orWheres hence the apparent roundabout way of going about this - the theory is what's important):

                  $start_date = //some date;
                  
                  $prices = BenchmarkPrice::select('price_date', 'price')
                  ->orderBy('price_date', 'ASC')
                  ->where('ticker', $this->ticker)
                  ->where(function($q) use ($start_date) {
                  
                      // some wheres...
                  
                      $q->orWhere(function($q2) use ($start_date){
                          $dateToCompare = BenchmarkPrice::select(DB::raw('min(price_date) as min_date'))
                          ->where('price_date', '>=', $start_date)
                          ->where('ticker', $this->ticker)
                          ->pluck('min_date');
                  
                          $q2->where('price_date', $dateToCompare);
                      });
                  })
                  ->get();
                  

                  如您所見,我選擇發(fā)生在我的start_date 或之后的最早日期.這會導(dǎo)致運行單獨的查詢以獲取此日期,然后將其用作主查詢中的參數(shù).有沒有一種雄辯的方法可以將查詢嵌入在一起形成一個子查詢,從而只有 1 個數(shù)據(jù)庫調(diào)用而不是 2 個?

                  As you can see I pluck the earliest date that occurs on or after my start_date. This results in a seperate query being run to get this date which is then used as a parameter in the main query. Is there a way in eloquent to embed the queries together to form a subquery and thus only 1 database call rather than 2?

                  根據(jù)@Jarek 的回答,這是我的查詢:

                  As per @Jarek's answer this is my query:

                  $prices = BenchmarkPrice::select('price_date', 'price')
                  ->orderBy('price_date', 'ASC')
                  ->where('ticker', $this->ticker)
                  ->where(function($q) use ($start_date, $end_date, $last_day) {
                      if ($start_date) $q->where('price_date' ,'>=', $start_date);
                      if ($end_date) $q->where('price_date' ,'<=', $end_date);
                      if ($last_day) $q->where('price_date', DB::raw('LAST_DAY(price_date)'));
                  
                      if ($start_date) $q->orWhere('price_date', '=', function($d) use ($start_date) {
                  
                          // Get the earliest date on of after the start date
                          $d->selectRaw('min(price_date)')
                          ->where('price_date', '>=', $start_date)
                          ->where('ticker', $this->ticker);                
                      });
                      if ($end_date) $q->orWhere('price_date', '=', function($d) use ($end_date) {
                  
                          // Get the latest date on or before the end date
                          $d->selectRaw('max(price_date)')
                          ->where('price_date', '<=', $end_date)
                          ->where('ticker', $this->ticker);
                      });
                  });
                  $this->prices = $prices->remember($_ENV['LONG_CACHE_TIME'])->get();
                  

                  orWhere 塊導(dǎo)致查詢中的所有參數(shù)突然變得不帶引號.例如.WHEREprice_date>= 2009-09-07.當(dāng)我刪除 orWheres 時,查詢工作正常.這是為什么?

                  The orWhere blocks are causing all parameters in the query to suddenly become unquoted. E.g. WHEREprice_date>= 2009-09-07. When I remove the orWheres the query works fine. Why is this?

                  推薦答案

                  這是您在以下位置執(zhí)行子查詢的方式:

                  This is how you do a subquery where:

                  $q->where('price_date', function($q) use ($start_date)
                  {
                     $q->from('benchmarks_table_name')
                      ->selectRaw('min(price_date)')
                      ->where('price_date', '>=', $start_date)
                      ->where('ticker', $this->ticker);
                  });
                  

                  不幸的是orWhere需要明確提供$operator,否則會引發(fā)錯誤,所以在你的情況下:

                  Unfortunately orWhere requires explicitly provided $operator, otherwise it will raise an error, so in your case:

                  $q->orWhere('price_date', '=', function($q) use ($start_date)
                  {
                     $q->from('benchmarks_table_name')
                      ->selectRaw('min(price_date)')
                      ->where('price_date', '>=', $start_date)
                      ->where('ticker', $this->ticker);
                  });
                  

                  <小時>

                  實際上你需要在閉包中指定from,否則它不會構(gòu)建正確的查詢.


                  You need to specify from in the closure in fact, otherwise it will not build correct query.

                  這篇關(guān)于如何使用 Laravel Eloquent 創(chuàng)建子查詢?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標(biāo)不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動程序)
                1. <small id='k5h4S'></small><noframes id='k5h4S'>

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

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

                            主站蜘蛛池模板: 韩国久久精品 | 二区三区视频 | 91在线精品视频 | 亚洲国产午夜 | www.玖玖玖 | 日韩不卡三区 | 日日干天天操 | 成人精品一区亚洲午夜久久久 | 欧美日韩免费一区二区三区 | 久久久久久国 | 精品一区二区三区在线视频 | 玖玖久久| 国产目拍亚洲精品99久久精品 | 日本免费一区二区三区 | 激情久久久久 | 国产精品二区三区 | 久久久久久美女 | 日韩欧美视频 | 秋霞精品 | 欧美色综合天天久久综合精品 | 成人伊人| 91免费版在线观看 | 久久久久久久久久久高潮一区二区 | h视频在线免费 | 极品在线| 国产 日韩 欧美 在线 | 久综合| 在线国产视频 | 欧美亚洲国产一区二区三区 | 国产精品国产三级国产aⅴ中文 | 亚洲欧美日韩精品 | 香蕉久久久 | 欧美日韩高清在线一区 | 蜜桃在线一区二区三区 | 伊人网综合在线观看 | 欧美精品久久久 | 亚洲视频在线一区 | 国产精品久久九九 | 久久精品91久久久久久再现 | 蜜桃一区二区三区 | 免费成年网站 |