【问题标题】:Pull count of records in a table that have the same value with Codeigniter Active Record拉取表中与 Codeigniter Active Record 具有相同值的记录数
【发布时间】:2014-01-16 03:11:22
【问题描述】:

我在一个网站上工作,那里有一张桌子,上面有“渔获”(人们用特殊的诱饵钓到了鱼)。 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,

【问题讨论】:

  • 实际的数据库架构会有所帮助
  • 那不是真的必要,我有一个包含“user_catches”表的数据库。表中有一行“lure_used”,其类型为:整数并包含一个诱饵 ID。该表中的其他内容均不重要。它有大约 30 个不相关的字段,只会使问题复杂化。
  • 我喜欢人们提问时告诉我我需要知道什么才能回答它。
  • 大声笑好吧,我想我是个白痴。了解所有其他字段,例如捕获日期和钓竿和卷轴定义。在这里帮助你。哈哈。我会把它贴在上面。

标签: php mysql database codeigniter activerecord


【解决方案1】:
SELECT lure, count (*) as count
from user_catches
GROUP BY lures
ORDER BY count;

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 2015-01-04
    • 1970-01-01
    • 2013-06-14
    • 2010-11-16
    • 2013-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-07-03
    相关资源
    最近更新 更多