• <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 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>

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

    2. <tfoot id='DtUjx'></tfoot>
        <bdo id='DtUjx'></bdo><ul id='DtUjx'></ul>

        如何使用 Laravel Eloquent 创建子查询?

        时间:2023-09-24
        <legend id='tPY04'><style id='tPY04'><dir id='tPY04'><q id='tPY04'></q></dir></style></legend>
            <tbody id='tPY04'></tbody>

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

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

                  <tfoot id='tPY04'></tfoot>
                • <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 id='tPY04'><tfoot id='tPY04'></tfoot><dl id='tPY04'><fieldset id='tPY04'></fieldset></dl></div>
                  本文介绍了如何使用 Laravel Eloquent 创建子查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下 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();
                  

                  如您所见,我选择发生在我的start_date 或之后的最早日期.这会导致运行单独的查询以获取此日期,然后将其用作主查询中的参数.有没有一种雄辩的方法可以将查询嵌入在一起形成一个子查询,从而只有 1 个数据库调用而不是 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?

                  根据@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 块导致查询中的所有参数突然变得不带引号.例如.WHEREprice_date>= 2009-09-07.当我删除 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?

                  推荐答案

                  这是您在以下位置执行子查询的方式:

                  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,否则会引发错误,所以在你的情况下:

                  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,否则它不会构建正确的查询.


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

                  这篇关于如何使用 Laravel Eloquent 创建子查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在使用 eloquent 时排除某些列 下一篇:如何在 Eloquent 中为列的名称设置别名

                  相关文章

                  最新文章

                • <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 id='k5h4S'><tfoot id='k5h4S'></tfoot><dl id='k5h4S'><fieldset id='k5h4S'></fieldset></dl></div>
                  1. <tfoot id='k5h4S'></tfoot>