代码符号中的活动记录求和 [英] active record summation in codeigniter

查看:176
本文介绍了代码符号中的活动记录求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出投票总数,并使用 codeigniter 和mysql。在我的投票表我有一个列,显示投票类型0下投票和1投票如何我可以生成一个类似这样

I want to find sum of votes and am using codeigniter and mysql. In my votes table i've a column which shows type of vote 0 for down vote and 1 for up vote how can i generate an sql that looks like this

SELECT sum(type) FROM `votes` WHERE questions_id=1

codeigniter数据库类

using codeigniter database class

干杯

推荐答案

$this->db->select('SUM(type) as score');
$this->db->where('question_id',1);
$q=$this->db->get('votes');
$row=$q->row();
$score=$row->score

您的 $分数变量现在包含类型的总和。

Your $score variable now contains the sum of the types for that particular question.

希望有所帮助!

这篇关于代码符号中的活动记录求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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