简体   繁体   English

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

[英]calculate average of a specific column in codeigniter select query

I want to calculate average of a specific column, for eg. 我想计算特定列的平均值,例如。 rating column for that i am writing the following query but it is giving me syntax error in the query, Please help to solve my problem. 我正在编写以下查询的“评级”列,但它在查询中给了我语法错误,请帮助解决我的问题。

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

尝试这个:

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

CodeIgniter by default protect field you can disable the protection by passing one extra second parameter. 默认情况下,CodeIgniter的保护字段可以通过传递一个额外的第二个参数来禁用保护。

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

as $this->db->select() accepts an optional second parameter. 因为$ this-> db-> select()接受可选的第二个参数。 If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks 如果将其设置为FALSE,则CodeIgniter不会尝试使用反引号保护您的字段或表名

For More info you can refer Active record documentation https://ellislab.com/codeigniter/user-guide/database/active_record.html 有关更多信息,您可以参考活动记录文档https://ellislab.com/codeigniter/user-guide/database/active_record.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM