且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Zend DB模型中使用'distinct'

更新时间:2023-11-16 23:44:58

使用与众不同的方法:

public function countFollowers($user_id) 
{
    $select = $this->select()
              ->distinct()
              ->where('user_id = ?', $user_id);

    $rowset = $this->fetchAll($select);
    $rowCount = count($rowset);

    return $rowCount;
}

在进行有问题的编辑后,获得用户的关注者数量.您实际上需要使用 group 不区分大小写.我已经测试了以下查询的工作原理,以获取要进行count()处理的数据,

After edit in question to get count of followers of a user. You actually need to use group NOT distinct. I have tested the following query works to fetch the data to be count()ed,

SELECT * FROM followers其中user_id = 1 GROUP BY user_id, follower_id

SELECT * FROM followers WHERE user_id = 1 GROUP BY user_id, follower_id

我还没有测试代码,但是类似的东西应该可以工作:

I have not tested the code, but something like this should work:

public function countFollowers($user_id) 
{
    $select = $this->select()
              ->where('user_id = ?', $user_id)
              ->group(array('user_id', 'follower_id')); 

    $rowset = $this->fetchAll($select);
    $rowCount = count($rowset);

    return $rowCount;
}