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

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

      <tfoot id='nlhX9'></tfoot>
    1. <small id='nlhX9'></small><noframes id='nlhX9'>

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

      1. Laravel 5 在 BETWEEN 中使用 OR 条件

        Laravel 5 using OR condition with BETWEEN(Laravel 5 在 BETWEEN 中使用 OR 条件)
            <tbody id='k0fL9'></tbody>

        1. <tfoot id='k0fL9'></tfoot>
            <bdo id='k0fL9'></bdo><ul id='k0fL9'></ul>

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

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

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

                • 本文介绍了Laravel 5 在 BETWEEN 中使用 OR 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能帮我在 laravel Eloquent 中构建以下查询,我真的很困惑使用 OR 条件和 between

                  SELECT * FROM tbl WHEREexisting_start BETWEEN $newStart AND $newEnd 或$newStart BETWEEN existing_start AND existing_end

                  我尝试使用像

                  whereBetween('existing_start',[$newStart,$newEnd])

                  但不知道如何使用 OR

                  解决方案

                  查询生成器提供了一个 orWhereBetween 方法,但它在 查询生成器文档.但是,您可以在 Laravel API 文档 中找到它.

                  <小时>

                  以下说明假设变量具有以下值:

                  $newStart = '1';$newEnd = '10';

                  <小时>

                  不幸的是,使用 orWhereBetween 作为第二个条件不适用于您的情况,因为 whereBetweenorWhereBetween 都会检查列值介于两个输入值之间.这对您的第一个条件来说很好,因为它会检查 existing_start 列值是否在 $newStart$newEnd 之间.所以这很好:

                  ->whereBetween('existing_start', [$newStart, $newEnd])

                  因为它将被编译为:

                  WHERE `existing_start` BETWEEN '1' 和 '10'

                  但是您的第二个条件想要检查来自 $newStart 的输入值是否在两列值 existing_startexisting_end 之间,并且有没有这样做的查询生成器方法.所以这行不通:

                  ->orWhereBetween($newStart, ['existing_start', 'existing_end'])

                  因为它会被编译为:

                  OR `1` BETWEEN 'existing_start' AND 'existing_end'

                  注意1周围的反引号`,因为MySQL会尝试查找名为1的列并抛出错误.><小时>

                  所以这里最好的选择是使用 orWhereRaw 和这样的绑定:

                  DB::table('tbl')->whereBetween('existing_start', [$newStart, $newEnd])->orWhereRaw('? BETWEEN existing_start AND existing_end', [$newStart])->get();

                  ? 将被替换为 $newStart 的值,该值将被正确引用和转义以避免 SQL 注入.

                  <小时>

                  当然,也可以选择使用两个分组条件来检查边界,这相当于您的 BETWEEN 条件:

                  DB::table('tbl')->whereBetween('existing_start', [$newStart, $newEnd])->orWhere(function ($query) use ($newStart) {$query->where('existing_start', '<=', $newStart);$query->where('existing_end', '>=', $newStart);})->get();

                  将编译为:

                  SELECT * FROM `tbl`在哪里`existing_start` BETWEEN '1' 和 '10' 或(`existing_start` <='1' AND `existing_end` >='1')

                  Hi can anyone help me building below query in laravel Eloquent i am really confuse in using OR condition with between

                  SELECT * FROM tbl WHERE
                  existing_start BETWEEN $newSTart AND $newEnd OR
                  $newStart BETWEEN existing_start AND existing_end
                  

                  i tried using like

                  whereBetween('existing_start',[$newSTart,$newEnd])
                  

                  but have no idea how to use OR

                  解决方案

                  There is an orWhereBetween method available from the Query Builder, but it is undocumented in the Query Builder Documentation. You can however find it in the Laravel API Documentation.


                  The explanations below assume that the variables have the following values:

                  $newStart = '1';
                  $newEnd = '10';
                  


                  Unfortunatelly, using orWhereBetween for the second condition is not applicable in your case, because both whereBetween and orWhereBetween will check if a column value is between two input values. This is fine from your first condition since it checks if the existing_start column value is between $newStart and $newEnd. So this is fine:

                  ->whereBetween('existing_start', [$newStart, $newEnd])
                  

                  As it will be compiled to:

                  WHERE `existing_start` BETWEEN '1' AND '10'
                  

                  However your second condition wants to check if an input value from $newStart is between two column values existing_start and existing_end, and there is no Query Builder method that does that. So this will not work:

                  ->orWhereBetween($newStart, ['existing_start', 'existing_end'])
                  

                  Because it will be compiled to:

                  OR `1` BETWEEN 'existing_start' AND 'existing_end'
                  

                  Notice the backticks ` around 1, because of that MySQL will try to find a column named 1 and throw an error.


                  So the best option here is to use orWhereRaw with bindings like this:

                  DB::table('tbl')
                    ->whereBetween('existing_start', [$newStart, $newEnd])
                    ->orWhereRaw('? BETWEEN existing_start AND existing_end', [$newStart])
                    ->get();
                  

                  The ? will be replaced by the value of $newStart which will be properly quoted and escaped to avoid SQL injection.


                  Or course there is always the option of having two grouped conditions that check the boundaries, which would be equivalent to your BETWEEN condition:

                  DB::table('tbl')
                    ->whereBetween('existing_start', [$newStart, $newEnd])
                    ->orWhere(function ($query) use ($newStart) {
                        $query->where('existing_start', '<=', $newStart);
                        $query->where('existing_end', '>=', $newStart);
                    })->get();
                  

                  Which will compile to:

                  SELECT * FROM `tbl`
                  WHERE
                    `existing_start` BETWEEN '1' AND '10' OR
                    (`existing_start` <= '1' AND `existing_end` >= '1')
                  

                  这篇关于Laravel 5 在 BETWEEN 中使用 OR 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
                  How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
                  Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                  Writing an SQL query to SELECT item from the following table(编写 SQL 查询以从下表中选择项目)
                  Converting MySQL code to Access: GROUP_CONCAT and a triple JOIN(将 MySQL 代码转换为 Access:GROUP_CONCAT 和三重 JOIN)
                  How can I convert an MDB (Access) file to MySQL (or plain SQL file)?(如何将 MDB (Access) 文件转换为 MySQL(或普通 SQL 文件)?)
                    <tbody id='s36Xy'></tbody>

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

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

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