时间戳转换器

雄辩的倍数 where from date fields <= >=

日期:2023-05-09     浏览:23    
【中文标题】雄辩的倍数 where from date fields <= >=【英文标题】:Eloquent multiple where from date fields <= >= 【发布时间】:2016-09-14 06:17:07 【问题描述】:

我想从 mySql 表中选择项目。 我使用 Laravel 5.2 & Eloquent

我的表结构: 编号 | item_id |姓名 |日期开始 |日期结束

id = 主要,自动 item_id = int 姓名 = 文字 date_start & date_end 为 'Date' 类型,格式为 '2016-09-14'

我的查询:

$dateFormated = date('Y-m-d',$tempDate);
$items = ItemList::where('item_id', $id)
  ->where('date_start', '>=', $dateFormated)
  ->where('date_end', '<=', $dateFormated)
  ->get()

如果我删除一个 where 子句,我会为所有具有正确 date_start/date_end 值的项目获得正确的结果。

我做错了什么,我该怎么办?

【问题讨论】:

试试between条件。 @urfusion : 据我所知,between 并没有比&gt;=, &lt;= 更完美。 @Virb 根据docs,两者之间做的完全一样 【参考方案1】:

你只需要交换运营商就可以了

  ->where('date_start', '<=', $dateFormated)
                         ^
  ->where('date_end',   '>=', $dateFormated)
                         ^

根据 cmets 中提供的数据,date_start 必须位于或之前 formattedDate 和date_end 之后,因此您的条件与应有的相反。


旧答案

  ->where('date_start', '>=', $dateFormated)
  ->where('date_end', '<=', $dateFormated)

除非您只想查找 1 个特定日期的记录,否则如何将开始日期和结束日期与同一日期进行比较?

语法很好,但逻辑可能不行。这样的查询会产生这样的结果

WHERE date_start>="2016-09-13" and date_end<="2016-09-13"

您确定要查找这些记录吗?

这正是您在删除与日期相关的 where 子句时获得良好结果的原因 - 这些事件不是在同一日期开始和结束。

正如 cmets 所证实的,这实际上是 的问题。 WHERE 子句不符合要求。解决方案是使用 orWhere

【讨论】:

是的,我确定...搜索日期必须在开始/结束日期的范围内。如果 start_date 是 2016-01-01 和 end_date 2016-01-31,则 $dateFormated 是 2016-01-12。 给出你的例子,你最终得到以下表达式 "2016-01-01">="2016-01-12" 和 "2016-01-13" 【参考方案2】:

这对我有帮助

    if($start_date!='')
            $q->whereDate('date_start', '>=', date('Y-m-d', strtotime($start_date)));
    
    if($end_date!='')
            $q->whereDate('date_end', '<=', date('Y-m-d', strtotime($end_date)));
    

试试看,希望能帮到你。

【讨论】:

对不起,没有。结果是一样的......空数组:(

相关文章

雄辩的计数出现与 where 子句

{】雄辩的计数出现与where子句【英文标题】:Eloquentcountoccurrencewithwhereclause【发布时间】:2022-01-0719:46:04【问题描述】:我正在尝试使用Eloquent进行简单的查询。我的test_registrants表看起来像这样我想用payment_status=1添加值全部为user...}

无法使用 Laravel 雄辩模型更新 json 列

{】无法使用Laravel雄辩模型更新json列【英文标题】:can\'tupdatejsoncolumnusingLaraveleloquentmodel【发布时间】:2021-06-0122:41:09【问题描述】:我正在尝试测试Eloquent模型的更新.../**@test*/publicfunctionupdates_to_json_fields_are_logged()$data=json_encode([...}

使用 where 子句检索数据时,无论如何要检索计数 0 吗? (Laravel 雄辩)

{...用where子句检索数据时,无论如何要检索计数0吗?(Laravel雄辩)【英文标题】:Isthereanywaytoretrievecount0whenretrievedatausingwhereclause?(LaravelEloquent)【发布时间】:2022-01-1215:46:23【问题描述】:我目前正在使用groupby检索数据,以获得相同...}

在雄辩的 Where 子句中使用类似通配符的语法在两个字符串之间进行搜索

{】在雄辩的Where子句中使用类似通配符的语法在两个字符串之间进行搜索【英文标题】:Wildcard-likesyntaxinaneloquentWhereclausetosearchbetweentwostrings【发布时间】:2021-12-2123:49:15【问题描述】:我看到了这个问题Wildcard-likesyntaxinaneloquentWh...}

雄辩 |添加自定义值以选择

{】雄辩|添加自定义值以选择【英文标题】:Eloquent|Addacustomvaluetoselect【发布时间】:2021-11-0816:54:30【问题描述】:我有多个表,它们都保存着不同的交易数据,这里是一个例子:$q1=DB::connection(\'mysql_live\')->table(\'transactions_vk\')-...}

雄辩 - 不等于

{】雄辩-不等于【英文标题】:Eloquent-wherenotequalto【发布时间】:2015-03-3104:06:06【问题描述】:我目前使用的是最新的Laravel版本。我尝试了以下查询:Code::where(\'to_be_used_by_user_id\',\'<>\',2)->get()Code::whereNotIn(\'to_be_used_by_user_id...}

Laravel 雄辩与条件

{】Laravel雄辩与条件【英文标题】:Laraveleloquentwithandwithcondition【发布时间】:2021-12-1809:27:44【问题描述】:我正在尝试从类别中获取类别和选定项目。这是我的代码:$reqItems=$request->items;//array-selecteditem$categories=Category::where(\'ty...}

拉拉维尔 |雄辩的 foreach 不起作用

{】拉拉维尔|雄辩的foreach不起作用【英文标题】:laravel|eloquentforeachnotworking【发布时间】:2017-09-0210:11:09【问题描述】:我正在尝试从我的用户表中检索所有非管理员用户。像这样$agents=User::where(\'is_admin\',\'=\',\'false\')->get();//di...}

雄辩的ORM laravel 5获取ID数组

{】雄辩的ORMlaravel5获取ID数组【英文标题】:EloquentORMlaravel5GetArrayofids【发布时间】:2016-03-2208:41:10【问题描述】:我使用的是EloquentORMlaravel5.1,我想返回一个大于0的id数组,我的模型叫做test。我试过了:$test=test::select(\'id\')->w...}

Laravel 4,雄辩 - 语句和运算符之间

{】Laravel4,雄辩-语句和运算符之间【英文标题】:Laravel4,eloquent-betweenstatementandoperators【发布时间】:2013-12-1209:47:11【问题描述】:有一个我用来在mysql中运行的查询:select*frommy_tablewhere$valbetweencol1andcoL2;它工作正常,但使用laravel...}

Copyright ©2021 时间戳转换器 小常识 114pp | 陕ICP备18005036号