我的问题是,我想从数据库表中按年份和月份获取created_at
属性的数据。我尝试过的代码是:
$post= Mjblog::select(DB::raw('YEAR(created_at) year, MONTH(created_at) month'));
$posts_by_y_m = $post->where('created_at',$post)->get();
我的问题是,我想从数据库表中按年份和月份获取created_at
属性的数据。我尝试过的代码是:
$post= Mjblog::select(DB::raw('YEAR(created_at) year, MONTH(created_at) month'));
$posts_by_y_m = $post->where('created_at',$post)->get();
查询构造器中提供了日期助手:
$post = Mjblog::whereYear('created_at', '=', $year)
->whereMonth('created_at', '=', $month)
->get();
->whereMonth('created_at', $month)
。两种方法都可以。 - Samir Mammadhasanov虽然被接受的答案可能解决了OP的问题,但从数据库性能的角度来看,这并不是最佳解决方案。因为当应用whereYear()
或whereMonth()
辅助函数查询记录时,它会使查询变得非SARGable。这意味着如果比较的列created_at
在数据库中有索引,则在搜索数据时将忽略此索引。请参见什么使SQL语句sargable?
考虑以下表达式
$posts = Mjblog::whereYear('created_at', '=', $year)
->whereMonth('created_at', '=', $month)
->get();
生成的查询将会像这样
select *
from mjblog
where year(`created_at`) = :year
and month(`created_at`) = :month
created_at
上应用了year()
和month()
函数,从而产生了一个非索引值。$year = 2000;
$month = 2;
$date = \Carbon\Carbon::parse($year."-".$month."-01"); // universal truth month's first day is 1
$start = $date->startOfMonth()->format('Y-m-d H:i:s'); // 2000-02-01 00:00:00
$end = $date->endOfMonth()->format('Y-m-d H:i:s'); // 2000-02-29 23:59:59
现在,可搜索的表达式可以写成:
select *
from mjblog
where created_at between :start and :end
或者
select *
from mjblog
where created_at >= :start
and created_at <= :end
$posts = Mjblog::whereBetween('created_at', [$start, $end])
->get();
或者
$posts = Mjblog::where('created_at', '>=', $start)
->where('created_at', '<=', $end)
->get();
另一篇有用的文章,突出了非SARGable谓词和反模式的缺点。
created_at
是索引列,那么这种方法会利用添加的索引,而另一种方法则会忽略它,因为查询将应用于year()和month()函数,从而产生与索引值不同的新值。 - M Khalid Junaid如果您想从一个Mjblog
实例中获取年份和月份,可以按照以下方式访问:
$year = $post->created_at->year;
$month = $post->created_at->month;
阅读有关Carbon\Carbon
获取器文档的更多信息。
$post->where('created_at',$post)->get();
? - Mustafa Ehsan Alokozay