gpt4 book ai didi

php - 在 sql codeigniter 中查询

转载 作者:行者123 更新时间:2023-12-01 00:27:35 28 4
gpt4 key购买 nike

我有一张表 tickets(username,posts,scheme)用户可能有帖子。例如,

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

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

_______________________
|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');
}

但这似乎不起作用:(

最佳答案

您应该使用 $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');
}

关于php - 在 sql codeigniter 中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11683675/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com