【发布时间】:2016-10-15 05:37:57
【问题描述】:
我正在使用 codeigniter 3.1.0,我试图从不同的表中获取两列的总和
如果我的 user_id = 1 它应该返回 421 的总和但返回 431 它正在添加我的其他用户的投票回复表
问题使用 Active Record 如何确保 SUM 获得正确的用户 ID 数量。
模型函数
public function thread_reputation($user_id) {
$this->db->select('SUM(reply.votes + thread.votes) AS reputation');
$this->db->from('thread', 'left');
$this->db->join('reply', 'reply.user_id = thread.user_id', 'left');
$this->db->where('thread.user_id', $user_id);
$this->db->where('reply.user_id', $user_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
} else {
return 0;
}
}
我在其中回显它的控制器部分
$thread_reputation = $this->thread_analytics_model->thread_reputation('1');
echo $thread_reputation->reputation;
线程表
回复表
【问题讨论】:
标签: codeigniter