我想得到日期范围created_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在我的控制器里
$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的所有数据,如何只获得status2
我尝试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'发布于 2018-05-11 06:57:10
您可以尝试对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. '%');
});发布于 2018-05-11 06:59:06
Table::where(function($q) use ($from,$to)){
$q->whereBetween('created_at', [$from, $to])
->orwhere('created_at', 'like', '%' .$to. '%')
}->where('status', 2);您可以搜索“嵌套/子查询雄辩的laravel”以获得更多信息。
https://stackoverflow.com/questions/50286588
复制相似问题