使用Codeigniter Active Record拉取表中具有相同值的记录数

时间:2014-01-16 03:11:22

标签: php mysql database codeigniter activerecord

我正在一个网站上工作,我有一张带有“捕获”的桌子(人们捕获了特别诱饵的鱼)。 lure_used类别是一个整数。

我想找到用户“最好的诱惑”。所以我想循环遍历所有user_catches行并返回一个具有每个诱饵的数组以及使用它的次数。

例如,我们可能有

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

所以从那以后我想知道5和6发生1次而3发生两次。因此3是最成功的诱惑。

我知道我可能在mysql查询中使用“MAX”,但我并不熟悉它并尝试失败。

我已经能够创建一个使用的诱饵数组,如:

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

所以也许我可以循环通过它并输出类似......

的内容
5 = 1, 3 = 2, 6 = 1.

我想我正在抓住稻草哈哈,谁都对如何完成这件事有更好的了解?

以下是“user_catches”表中的所有字段:

'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,

1 个答案:

答案 0 :(得分:0)

SELECT lure, count (*) as count
from user_catches
GROUP BY lures
ORDER BY count;