简体   繁体   English

从相关表中获取第二高的日期

[英]Get second highest date from related table

I am trying to fetch the second highest date from a relation-table in with Eloquent Laravel 6.我正在尝试使用 Eloquent Laravel 6 从关系表中获取第二高的日期。

MoodTable
id    animal_id  date          mood
1     1          2019-12-14    happy
2     1          2019-12-11    drunk
3     1          2019-12-13    sad

AnimalTable
id    name
1     Dog

So for example, I want to be able to query: "Today Dog is happy. Prevously to that he was Drunk.例如,我希望能够查询:“今天狗很高兴。之前他喝醉了。

To fetch the highest I use:要获取我使用的最高值:

return $this->hasMany('App\AnimalMood', 'animal_id')->select(DB::raw('
                    mood,
                    animal_id,
                    MAX(date) as max_date,  
                    '))
                 ->groupBy('stock_id');

However, when it comes to seconed highest date... I'm out...然而,当谈到第二个最高日期时......我出去了......

I have looked at How to return second newest record in SQL?我看过如何在 SQL 中返回第二条最新记录? for some answers,but not been able to put that in a relationship senario nor translate it to Eloquent.对于某些答案,但无法将其放入关系场景中,也无法将其翻译为 Eloquent。

Ideally, I would like to run Animal::with('moodPrevious')->get() and Animal::find(1)->moodPrevious from my controller...理想情况下,我想从我的控制器运行Animal::with('moodPrevious')->get()Animal::find(1)->moodPrevious ...

I would change the relationship and grab all of the related models to your Animal model when you query for it.当您查询它时,我会更改关系并将所有相关模型获取到您的Animal模型。

return $this->hasMany('App\\AnimalMood', 'animal_id');

Below returns the animals with the moods pre-sorted.下面返回情绪预先排序的动物。

$animals = Animal::with(['mood' => function($q){
               $q->orderByDesc('date');
           }])->get();

Laravel offers a lot of collection methods that work well with related models. Laravel 提供了很多与相关模型配合良好的收集方法。 I would use the callback you can provide to first() in order to get the mood you want on the model.我会使用你可以提供给first()的回调来获得你想要的模型情绪。 You probably already have the specific model you want, either by foreaching over the above collection or something similar.您可能已经拥有了您想要的特定模型,无论是通过对上述集合进行 foreach 还是类似的东西。 The relation will be a collection instance so we use the collection method to get the mood desired.该关系将是一个collection实例,因此我们使用集合方法来获得所需的mood

$previousMood = $animal->mood->first(function($value, $key){
    return $key == 1 // You can use whatever here, this will return the second item on the relation, or the previous mood.
});

See https://laravel.com/docs/5.8/collections#method-first for a reference.请参阅https://laravel.com/docs/5.8/collections#method-first以获取参考。

I think this would work我认为这会奏效

return $this->hasMany('App\AnimalMood', 'animal_id')
    ->select(
        DB::raw('
            mood,
            animal_id,
            MAX(date) as max_date,  
        ')
    )
    ->where(
        DB::raw("
            date = (
                SELECT 
                    MAX(date) 
                FROM animal_moods 
                WHERE date < (
                    SELECT 
                        MAX(date) 
                    FROM 
                    animal_moods
                )
            )
        ")
    )
    ->groupBy('stock_id');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM