简体   繁体   English

如何根据 Laravel-8 列中的每个值从数据库表中获取最新记录?

[英]how to get latest record from database table against each value in column Laravel-8?

I have been working on a project and a bit stuck in fetching record from table and need some help in this regard.我一直在做一个项目,有点卡在从表中获取记录,在这方面需要一些帮助。 Now The scenario is I have a table similar like below:现在的情况是我有一个类似下面的表:

id col_1        col_2               created _at         updated_at
1      1        some value          timestamps          timestamps
2      1        some new value      timestamps          timestamps
3      2        some value          timestamps          timestamps  
4      2        some new value      timestamps          timestamps

Now as u see in col_1 there are values numbers repeating, every time a value against them is inserted.现在,正如您在 col_1 中看到的那样,每次插入针对它们的值时,都会有重复的值数字。 Now what I am working on and stuck in is fetching latest record against each values in col_1.现在我正在研究并坚持的是针对 col_1 中的每个值获取最新记录。 So that my result look like this所以我的结果看起来像这样

id col_1        col_2               created _at         updated_at
2      1       some new value       timestamps          timestamps
4      2       some new value       timestamps          timestamps

Below is my piece of code what I tried but get me wrong data.下面是我尝试过的一段代码,但得到了错误的数据。

  $data = TableName::select(DB::raw('t.*'))
            ->from(DB::raw('(SELECT * FROM table_name) t'))
            ->groupBy('t.col_2')->latest()
            ->first();
        dd($data);
        return view('view_name')->with([
            'data' => $data
        ]);
    }

Any idea how can I achieve such thing in Laravel.知道如何在 Laravel 中实现这样的事情。 Any help would be highly appreciated.任何帮助将不胜感激。 Thanks谢谢

I think the following should give you the results you want.我认为以下内容应该会给您想要的结果。

$data = DB::table('table_name')
  ->groupBy('col_1')
  ->latest()
  ->get();

If the TableName model is associated with the correct table (check if the query is correct when you do dd(TableName::query()->toSql()); ), then you can replace the query with如果 TableName model 与正确的表相关联(在执行dd(TableName::query()->toSql());时检查查询是否正确),则可以将查询替换为

$data = TableName::groupBy('col_1')->latest()->get();

In a technical sense, when you group rows, the columns that are not/cannot be grouped lose meaning.从技术意义上讲,当您对行进行分组时,未/不能分组的列失去了意义。 So in situations like this, you could or rather you should be looking to do this in two separate queries (especially if SQL full group only mode is on).因此,在这种情况下,您可以或者更确切地说应该在两个单独的查询中执行此操作(特别是如果 SQL 仅全组模式已打开)。 One to group, the other one to join the actual record.一个组,另一个加入实际记录。

See if something like this works:看看这样的事情是否有效:

$grouped = Model::select('col_id as merged_col_id', DB::raw('max(id) as latest_id'))->groupBy('col_id'); //since `id` field looks autoincrementing, using max(id) here should give you the latest record from all merged columns.

return Model::select('id', 'col_id', 'val')->joinSub($grouped, 'grouped', function($join){
    $join->on('grouped.latest_id', '=','table_name.id');
})->get();

Take care to change Model and table_name appropriately.注意适当地更改Modeltable_name

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

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