久久久久久久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)建子查詢?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有以下 Eloquent 查詢(這是一個(gè)查詢的簡(jiǎn)化版本,它由更多的 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 或之后的最早日期.這會(huì)導(dǎo)致運(yùn)行單獨(dú)的查詢以獲取此日期,然后將其用作主查詢中的參數(shù).有沒有一種雄辯的方法可以將查詢嵌入在一起形成一個(gè)子查詢,從而只有 1 個(gè)數(shù)據(jù)庫調(diào)用而不是 2 個(gè)?

                  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ù)突然變得不帶引號(hào).例如.WHEREprice_date>= 2009-09-07.當(dāng)我刪除 orWheres 時(shí),查詢工作正常.這是為什么?

                  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,否則會(huì)引發(fā)錯(cuò)誤,所以在你的情況下:

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

                  <小時(shí)>

                  實(shí)際上你需要在閉包中指定from,否則它不會(huì)構(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)建子查詢?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(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 可滾動(dòng)游標(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 獲取一個(gè)值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動(dòng)程序)
                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>

                            主站蜘蛛池模板: 亚洲精品视频在线播放 | 亚洲欧美综合 | 一级a性色生活片久久毛片 午夜精品在线观看 | 久草精品视频 | 国产精品日本一区二区在线播放 | 日本午夜免费福利视频 | 中文字幕在线观看一区二区 | 一区二区三区四区免费在线观看 | 国产在线一区观看 | 色婷婷av一区二区三区软件 | 久久精品国产久精国产 | 久久久久国产一区二区三区四区 | 精品二区视频 | 亚洲第一黄色网 | 超碰在线播 | 亚洲天堂成人在线视频 | 午夜影晥 | 亚洲国产成人精品一区二区 | 国产精品高清在线 | 毛片视频观看 | 亚洲国产一区在线 | h片在线观看免费 | 中文字幕av网站 | 日韩二三区 | 国产精品欧美一区二区 | 日韩久久久久久 | 久久久久久久久久久高潮一区二区 | 日韩欧美中文在线 | 四虎影视| 色啪网 | 国产精品一区二区视频 | 免费麻豆视频 | 亚洲高清视频一区二区 | cao在线 | 欧美日本一区二区 | 中文字幕一区二区三区四区五区 | 精品国产欧美一区二区三区成人 | 精品亚洲一区二区三区四区五区 | 久久大陆 | www国产亚洲精品 | 久久久久成人精品亚洲国产 |