计算 codeigniter select 查询中特定列的平均值

calculate average of a specific column in codeigniter select query

我想计算特定列的平均值,例如。我正在编写以下查询的评级列,但在查询中出现语法错误,请帮助解决我的问题。

$this->db->select('*, AVG(`rating`) As avg_r');

试试这个:

$this->db->select('id, name, email, AVG(rating) as avg_r');

CodeIgniter 默认保护字段,您可以通过传递一个额外的第二个参数来禁用保护。

$this->db->select('*, AVG(`rating`) As avg_r',FALSE);

as $this->db->select() 接受可选的第二个参数。如果您将其设置为 FALSE,CodeIgniter 将不会尝试保护您的字段或 table 带有反引号

的名称

有关更多信息,您可以参考活动记录文档 https://ellislab.com/codeigniter/user-guide/database/active_record.html