简体   繁体   中英

Pull count of records in a table that have the same value with Codeigniter Active Record

I am working on a site where I have a table with "catches" (people have caught fish with a particular lure). The lure_used category is an integer.

I want to find the users "best lure". So i want to loop through all the user_catches rows and return an array that has each lure and how many times it was used.

So for example we might have

catch_1 | lure_used = 5,
catch_2 | lure_used = 3,
catch_3 | lure_used = 6,
catch_4 | lure_used = 3,

so from that i would like to know that 5 and 6 occur 1 time while 3 occurs twice. Therefore 3 is the most successful lure.

I know that I could probably use "MAX" in mysql queries, but i'm not really farmiliar with it and attempts have failed.

I have been able to create an array of lures used like:

$lures = array(5, 3, 6, 3);

So maybe i could just loop through that and output something like...

5 = 1, 3 = 2, 6 = 1.

I guess i'm grasping at straws haha, anyone have a better idea on how to get this done?

Here are all the fields in the "user_catches" table:

'id' => $row->id,
            'user_id' => $row->user_id,
            'species' => $row->species,
            'season' => $row->season,
            'lure_used' => $row->lure_used,
            'depth' => $row->depth,
            'exact_depth' => $row->exact_depth,
            'presentation' => $row->presentation,
            'catch_weight' => $row->catch_weight,
            'length' => $row->length,
            'fishing_from' => $row->fishing_from,
            'moon_phase' => $row->moon_phase,
            'water_temp' => $row->water_temp,
            'water_quality' => $row->water_quality,
            'notes' => $row->notes,
            'rod' => $row->rod,
            'reel' => $row->reel,
            'line' => $row->line,
            'rig' => $row->rig,
            'catch_image' => $row->catch_image,
            'weather' => $row->weather,
            'temp' => $row->temp,
            'humidity' => $row->humidity,
            'wind' => $row->wind,
            'pressure' => $row->pressure,
            'visibility' => $row->visibility,
            'location' => $row->location,
            'weather_icon' => $row->weather_icon,
            'catch_date' => $row->catch_date,
SELECT lure, count (*) as count
from user_catches
GROUP BY lures
ORDER BY count;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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