У меня есть красноречивая модель "Атлет" и есть еще один настольный перформанс.Каждый спортсмен имеет от 0 до многих выступлений.И я хотел бы получить лучшую производительность каждого спортсмена (личный рекорд) или ноль, если у спортсмена еще нет выступлений.
Моя модель спортсмена:
class Athlete extends Model
{
// I would like to do something like
public $personalBest = max(performances) - the highest perfomance
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'athletes';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* Get the performances for the Athelete post.
*
* @return HasMany
*/
public function performances()
{
return $this->hasMany('App\EloquentModels\Performance', 'athlete_id', "id");
}
}
Я хотел бы получитьвысочайшая производительность каждого спортсмена.Надеюсь, что это имеет смысл.
Я думаю, что где-то нужно ответить, но мне не повезло найти его.Извините, если я просто не смог его найти.
Performances table
id(int) year(int) performance(float)
-------------------------------------
1 2000 257.3
2 2001 227.3
Просто чтобы обернуть вещи.Публикация последнего необработанного запроса:
select [athletes].[first_name], [athletes].[last_name], MAX(performance) AS personal_best
from [athletes]
left join [performances] on [athletes].[id] = [performances].[athlete_id]
group by [athletes].[id], [athletes].[first_name], [athletes].[last_name]
order by [personal_best] desc