0

我有一张表门票(用户名,帖子,方案)用户可能有可能的帖子。例如,

____________________________________
username  |    posts   |    scheme |
__________|____________|___________|
A         |    post1   |      10   |
__________|____________|___________|
B         |    post2   |      2    |
__________|____________|___________|
B         |    post 3  |      13   |
__________|____________|___________|
A         |    post 4  |      21   |
__________|____________|___________|
A         |    post 5  |      -1   |
__________|____________|___________|

我的查询应该生成具有标题总数和总声誉的输出不同用户名。IE,

_______________________
|A   |   3   |   30    |
|____|_______|_________|
|B   |   2   |   15    |
|____|_______|_________|

我的模型功能:-

function getAllUsers(){                      
        $this->db->distinct();
        $this->db->select('username,COUNT(title) AS numtitle');
        $this->db->where('site_referers_id',1);
        return $this->db->get('tbl_tickets');
     }

但这似乎不起作用:(

4

3 回答 3

4

您应该使用$this->db->group_by()而不是distinct

function getAllUsers(){                      
  $this->db->select('username, COUNT(*) AS numtitle, SUM(scheme) AS total');
  $this->db->where('site_referers_id',1);
  $this->db->group_by('username');
  return $this->db->get('tbl_tickets');
}
于 2012-07-27T08:02:37.313 回答
2

检查此代码

    $this->db->select('username,COUNT(title) AS numtitle');
    $this->db->where('site_referers_id',1);
     $this->db->group_by('username');
    return $this->db->get('tbl_tickets');

你得到完美的结果

于 2012-07-27T08:12:11.243 回答
0

我知道这个问题很早以前就问过了,但是旧的答案并没有引起作者的注意。尝试使用 SQL:

$sql ="SELECT count(*),username,SUM(scheme) FROM `tble_one` group by username";
$result = $this->db->query($sql)->get();
于 2017-01-24T06:09:23.537 回答