php - 如何使用 codeigniter 查询获取列的总和

标签 php mysql codeigniter

我正在尝试使用 codeigniter 查询获取 mysql 表中列值的总和

$this->db
     ->query("Select SUM(tb1.amount) from table1 tb1 inner join table2 tb2 on tb2.Id=tb1.Id Where tb2.Status='1'")
     ->result()

它给我的错误为 array to string conversion我只需要一个这样的数字 count(amount) returns number of rows with num_row()

最佳答案

您可以使用codeigniter的select_sum()函数。尝试以下代码-

$query = $this->db->select_sum("tb1.amount")
                  ->from("table1 as tb1")
                  ->join("table2 as tb2","tb1.id = tb2.id") 
                  ->where("tb2.status",1)
                  ->get();
$query = $query->result();

$query = $this->db->query('SELECT sum(tb1.amount) FROM table1 as tb1 join table2 as tb2 on tb1.id = tb2.id where tb1.status = 1') ; $query = $query->result_array();

关于php - 如何使用 codeigniter 查询获取列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59223909/

相关文章:

php - 通过电子邮件字段查找联系人

PHP - 通过我自己的服务器,使用 HTTPS、cookie 和代理登录到远程服务器,并下载 html

php - 如何在变量参数函数中使用查询?

php - MyISAM 和 InnoDB 中的单个 mysql 语句是原子的吗?

php - 单击时获取 MySQL 数据 WHERE 列等于 HTML 链接

mysql - 如何进行 Codeigniter 拆分搜索

php - 如何将 codeigniter 函数名称引用为 native php 函数的回调?

php按多类别和一一过滤

php - 使用 PHP 中表中的按钮更新 mysql 数据库

codeigniter - 出于安全原因,error_reporting() 已被禁用