如何使用codeigniter查询获取列的总和 [英] how to get sum of a column with codeigniter query

查看:38
本文介绍了如何使用codeigniter查询获取列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get sum of a column values in mysql table with the codeigniter query as

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

它给我一个错误,因为数组到字符串转换我只需要一个数字,例如 count(amount)返回带有num_row()的行数

it give me error as array to string conversion I need just a number such count(amount) returns number of rows with num_row()

推荐答案

用作=> $ data ['total']

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

这篇关于如何使用codeigniter查询获取列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆