gpt4 book ai didi

php - whereBetween 和 orWhere 获取日期范围内的数据

转载 作者:行者123 更新时间:2023-11-29 04:33:52 27 4
gpt4 key购买 nike

我想获取创建的日期范围_at 这里是我的示例表

id    status  created_at
1 1 2018-05-10 10:53:54
2 2 2018-05-10 10:53:54
3 2 2018-05-9 11:53:54

在我的 Controller 中

$this->to = 2018-05-10;
$this->from = 2018-05-09;
Table::whereBetween('created_at', [$this->from, $this->to])
->orwhere('created_at', 'like', '%' .$this->to. '%')
->where('status', 2);

在这个查询中,它打印所有状态为 1 和 2 的数据,我怎么才能只获得状态 2

我尝试 die_dump 我的 sql 我得到这样的查询

select * from `table` where `created_at` between '2018-05-09' and '2018-05-10' or `created_at` like '%2018-05-10%' and `status` = '2'

最佳答案

您可以尝试 whereBetween('created_at', [$this->from, $this->to])->orwhere('created_at', 'like' , '%' .$this->to. '%')

$from = $this->from;
$to = $this->to;
Table::where('status', 2)
->where(function ($query) use ($from,$to) {
$query->whereBetween('created_at', [$from, $to])
->orwhere('created_at', 'like', '%' .$to. '%');
});

关于php - whereBetween 和 orWhere 获取日期范围内的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50286588/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com