php - 提取表中与 Codeigniter Active Record 具有相同值的记录计数

标签 php mysql database codeigniter activerecord

我正在一个网站上工作,那里有一张“捕获”的 table (人们用特定的诱饵捕获了鱼)。 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,

最佳答案

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

关于php - 提取表中与 Codeigniter Active Record 具有相同值的记录计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21152678/

相关文章:

php - 如何修复 Laravel 8 UI 分页问题?

sql - 为什么我的重音字符在 SQL Server 2005 中会出现断线?

javascript - 使用 Ajax 将密码发送到 PHP

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

Mysql按desc排序错误

javascript - 将 JS var 存储到数据库中

mysql - 阻止空白值输入数据库

c# - Entity Framework 代码优先中的数据库初始化

php - paypal 不发送实时 ipn 详细信息

php - 如何在 Laravel 中将 INFO 记录到单独的文件中