skip to Main Content

I have a table in my db that looks like this:

Col 1 Col 2
John 230601
Frank 230601
Tom 230604
Bill 230604
Tina 230604
Leroy 230602
Ernie 230601
Sue 230601

I want to get all rows with the max col2 value. (in this case max value is 230604 and it would be Tom, Bill and Tina).

I tried the below which failed:

$data = DB::table($table)->where('col2','=','(SELECT max(col2) from '.$table.')')->get($fields);

2

Answers


  1. This should do

    $maxValue = DB::table($table)->max('col2');
    $data = DB::table($table)->where('col2', $maxValue)->get($fields);
    
    Login or Signup to reply.
  2. You can use Model::max("col2");

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search