当我有这个查询时,我有一个用户配置文件列表。 我想为每个(每个)用户检索nr个关注者,但我只从查询中获得一行结果(有很多用户)
$this->db->select('up.name, up.id, up.image, up.name_id, up.created, COUNT(followers.user_id) as followed_by_count');
$this->db->join('user_profiles up', 'u.id = up.user_id', 'left');
$this->db->join('followers', 'up.user_id = followers.user_id', 'left');
//Something like this? (It doesn't work)
//$this->db->group_by('COUNT(followers.user_id) as followed_by_count');
//When tried this I get:
//"You have an error in your SQL syntax; check the manual that corresponds to your
//MySQL server version for the right syntax to use near 'as followed_by_count"
$this->db->order_by('up.created', 'DESC');
我想我应该以某种方式使用group by
?不使用followers-table时查询有效。它也适用于不使用group_by但我显然没有得到我想要的计数。
答案 0 :(得分:1)
更改
$this->db->group_by('COUNT(followers.user_id) as followed_by_count');
要
$this->db->group_by('u.id');
注意:此代码根据您给定的代码给出,并假设您的代码在语法上是正确的。