gpt4 book ai didi

php - 使用 Codeigniter 计算表中的平均评分

转载 作者:行者123 更新时间:2023-11-29 21:26:20 25 4
gpt4 key购买 nike

我有一个名为 company_reviews 的表,其中存储用户提交的评论和评级编号 (1-5)。我需要计算许多评论之间的平均值,并仅输出一位小数的总数(例如 2.5、3.5、2.1、5.1 等)。换句话说,我需要计算每个 company_reviews_ rating 的总数相关,其中 company_reviews_company_idid = x

表格结构

+--------------------+----------------------------+------------------------+
| company_reviews_id | company_reviews_company_id | company_reviews_rating |
+--------------------+----------------------------+------------------------+
| 1 | 1 | 1 |
| 2 | 1 | 5 |
| 3 | 1 | 4 |
| 4 | 1 | 4 |
| 5 | 1 | 3 |
| 6 | 1 | 2 |
+--------------------+----------------------------+------------------------+

这是我到目前为止所想出的,但我被困在这里

public function count_total_rating($id = NULL) {
$this->db->select('*');
$this->db->where('company_reviews_company_id', $id);
$this->db->from('company_reviews');
$query = $this->db->get();
}

如何在 Codeigniter 模型中做到这一点?

最佳答案

public function count_total_rating($id = NULL) {
$this->db->select('AVG(company_reviews_rating) as average');
$this->db->where('company_reviews_company_id', $id);
$this->db->from('company_reviews');
$query = $this->db->get();
}

希望对你有帮助

关于php - 使用 Codeigniter 计算表中的平均评分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35484687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com