Categories: php, sql, laravel, laravel-5, eloquent

Get maximum values in column with diferents foreign ids in Laravel

2 answers

I want to get the largest value from a column, but those values ​​correspond to two different foreign ids, so I want it to return the maximum number for each foreign id. I don't know If I have to use max()... At the moment I am just using groupBy with the two foreign ids.

Here the table in MySqlAdmin. I want to get the maximum bid for each id_group and id_karatekas: enter image description here

And here is my code:

public function soldKarateka()     {         $response = array('code' => 400, 'error_msg' => []);        self::getBidByKaratekaGroup($response);        return response($response, $response['code']);     }       private function getBidByKaratekaGroup( & $response){         $bidsPerKarateka = DB::table('bids')             ->groupBy('id_group','id_karatekas')            ->get();              $response = array('code' => 200, 'Karatekas order by group' => $bidsPerKarateka);     } 

All answers to this question, which has the identifier 61138334

The best answer:

You should try below code for find max value. if your table name is bids then it will be ok otherwise change name as per your table name.

DB::table('bids') ->select([DB::raw('MAX(bids.bid) AS maxbid'), 'bids.id_group', 'bids.id_karatekas']) ->groupBy('bids.id_group', 'bids.id_karatekas') ->get()->toArray(); 

Did you try the max function?

I think the following snippet will work

$data = DB::table('bids')     ->select(DB::raw('MAX(bid) as bid'))     ->groupBy('id_group', 'id_karatekas')->get(); 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data