[英]Get the latest row each group based on max date
I am trying to select single from every group where date is max.我正在尝试从日期最大的每个组中选择 select。
I am trying to do with this bellow我正在尝试处理这个问题
|NO | effective | price | level_one| level_two
+---+-------------+----------+|+++++++++++|++++++++++++
|1 | 2011-12-01 | 34 | 1 | 2
|2 | 2011-16-01 | 34 | 1 | 2
|3 | 2011-18-01 | 3434 | 1 | 2
|4 | 2011-16-01 | 3554 | 1 | 3
Result should be结果应该是
|NO | effective | price | level_one| level_two
+---+-------------+----------+|+++++++++++|++++++++++++
|3 | 2011-18-01 | 3434 | 1 | 2
|4 | 2011-16-01 | 3554 | 1 | 3
But result come但结果来了
|NO | effective | price | level_one| level_two
+---+-------------+----------+|+++++++++++|++++++++++++
|3 | 2011-12-01 | 34 | 1 | 2
|4 | 2011-16-01 | 3554 | 1 | 3
tried with试过
$price = App\Price::with('others')
->orderBy('effective', 'Desc')
->groupBy('level_one','level_two')
->get();
Problem on your date format.您的日期格式有问题。 Normally eloquent and mysql use Ymd date format.
通常 eloquent 和 mysql 使用 Ymd 日期格式。
Use like this:像这样使用:
$price = App\Price::with('others')
->orderByRaw("DATE_FORMAT(effective,'%Y-%d-%m') DESC")
->groupBy('level_one','level_two')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.