我想计算特定列的平均值,例如评级列。我正在写下面的查询,但它在查询中给了我语法错误,请帮助解决我的问题。
$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);
因为$this->db->select()接受可选的第二个参数。如果您将其设置为FALSE,CodeIgniter将不会尝试使用反标记来保护您的字段或表名
有关更多信息,您可以参考活动记录文档https://ellislab.com/codeigniter/user-guide/database/active_record.html