laravel eloquent 查询组按最后一个 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26549345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:19:25  来源:igfitidea点击:

laravel eloquent query group by last id

laravellaravel-4

提问by Lucas Campos

Hi I'm trying to get the last register in DB for each equipment

嗨,我正在尝试为每个设备获取 DB 中的最后一个寄存器

I got like

我喜欢

id   | id_equip
-----+----------
   1 |    3
   2 |    3
   3 |    3
   4 |    2
   5 |    2

I want to query like the last id of each equip like:

我想像每个装备的最后一个 id 一样查询:

  id   | id_equip
  -----+----------
  3    |    3
  5    |    2

I tried this:

我试过这个:

$calibracao = $this->calibracao->orderBy('id', 'DESC')->groupBy('id_equip')->get();

thanks for the help!

谢谢您的帮助!

回答by damiani

A simple way, without join, using max:

一个简单的方法,不用join,使用max

$query = DB::table('equip')
    ->select(DB::raw('*, max(id) as id'))
    ->groupBy('id_equip')
    ->orderBy('id', 'asc')
    ->get();

(Assuming, of course, that you can count on your idto be in date order.)

(当然,假设您可以指望按id日期顺序排列。)



You can also do this with Eloquent, using a self-referencing one-to-one relationship. The query takes longer, but it's much more Laravel-like:

你也可以使用 Eloquent做到这一点,使用自引用的一对一关系。查询需要更长的时间,但它更像 Laravel:

Define the relationship in your model:

定义模型中的关系:

class myModel extends Eloquent {
    public function latestEquipment()
    {
        return $this->hasOne('myModel', 'id_equip', 'id_equip')->latest();
    }
}

(Note that in this case, we're using latest()so that we don't rely on the order of ids to determine the most recent entry, but rather on the created_atdate of each record. This is more accurate.)

(请注意,在这种情况下,我们使用的是latest()这样我们不依赖于ids的顺序来确定最近的条目,而是created_at根据每条记录的日期。这样更准确。)

To retrieve all the records, with their latest equipment entry:

要检索所有记录及其最新设备条目:

$latest = myModel::with('latestEquipment')->groupBy('id_equip')->get();
// loop over the results
foreach ($latest as $l) {
    if ($l->equipment) {
        echo('ID: ' . $l->id . 'ID_EQUIP: ' . $l->id->id_equip . '<br>');
    }
}

回答by Bogdan

I'm not sure if there's an easier way and this is kind of convoluted because of the join, but it should give the result you want:

我不确定是否有更简单的方法,由于 ,这有点令人费解join,但它应该给出您想要的结果:

DB::table('equip as e')
    ->select('e.*')
    ->join(DB::raw("(SELECT id_equip, MAX(id) id FROM equip GROUP BY id_equip) as _e"), function ($join) {
        $join->on('e.id', '=', '_e.id')->on('e.id_equip', '=', '_e.id_equip');
    })
    ->orderBy('id', 'asc')
    ->get();

回答by Lucas Campos

public function latest($equipamentos)
    {
        foreach ($equipamentos as $equip) 
        {
            $latest[$equip->cod] = DB::table('tb_calibracao')
                                ->where('cod_equipamento', $equip->cod)
                                ->where('parecer', '1')
                                ->orderBy('cod', 'Desc')
                                ->first();
        }

        return $latest; 
    }

That worked, thx for the help!

那行得通,谢谢您的帮助!