How to use 'distinct' in zend db model

I have been searching for a long time for this work to work.

I want to know how I use “stand alone” in the zend db model to make my choice for followers of a unique user.

My db model for counting subscribers for a user (here I need to add "excellent")

public function countFollowers($user_id) { $rowset = $this->fetchAll("user_id = $user_id"); $rowCount = count($rowset); if ($rowCount > 0) { return $rowCount; } else { return $rowCount; } } 

EDIT: This function is part of the class. Application_Model_DbTable_Followers extends Zend_Db_Table_Abstract '

My table structure

  • ID
  • article_id // Article ID written by user_id.
  • user_id // user_id article owner
  • follower_id // member following this article
  • date // date of travel

'user_id' can be written in various articles, a follower can follow various articles in the same author. I want to make a unique count of followers. As an example, what I want, if the follower follows 8 articles of one author, he needs to be compared with "1" in the account.

I hope this is clear enough to understand what I was striving for.

Yours faithfully,

Nikki

+7
source share
6 answers

Using various:

 public function countFollowers($user_id) { $select = $this->select() ->distinct() ->where('user_id = ?', $user_id); $rowset = $this->fetchAll($select); $rowCount = count($rowset); return $rowCount; } 

EDIT: after making changes to the question, to get the number of followers of the user. You really need to use the NOT distinct group. I checked the following queries to get the data for count () ed,

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; } 
+11
source

You can specify mysql functions in the 'from' function, which executes the select query function. To use the from function, you need to pass the table name as the first parameter, however passing $ this (your table model class) works fine.

 public function countFollowers($user_id) { $rowset = $this->fetchAll( $this->select() ->from($this, array('DISTINCT user_id')) ->where('user_id = ?', $user_id) ); return count($rowset); } 

[edit]

Based on your editing, a “group” may also work for you:

 public function countFollowers($user_id) { $rowset = $this->fetchAll( $this->select() ->where('user_id = ?', $user_id) ->group('user_id') ); return count($rowset); } 

This will group all matching user_id into one record. Therefore, if the user is found, it will return 1, otherwise 0.

+2
source

Fetching all the lines just to get the score hits me like overkill.

You can make an account using something like this:

 $select = $db->select(); $select->from('testcount', new Zend_Db_Expr('COUNT(id)')) ->where('user_id = ?', $someUserId); return $db->fetchOne($select); 
+2
source

do not write that:

  public function countFollowers($user_id) { $rowset = $this->fetchAll( $this->select() ->from($this, array('DISTINCT user_id')) ->where('user_id = ?', $user_id) ); return count($rowset); } 

But this:

 public function countFollowers($user_id) { $rowset = $this->fetchAll( $this->select() ->from($this, array('DISTINCT(user_id)')) ->where('user_id = ?', $user_id) ); return count($rowset); } 

Otherwise, you will have an error similar to the Mysqli prepare error:

Unknown column 'repertoire.distinct idRepertoireParent' in 'list of fields'

+1
source

Today I tried DISTINCT in the JOIN LEFT case, and it does not work. But if you add the group to the DISTINCT column, it will work fine.

0
source

We also have one method from the official guide.

Just use "different"

Build this query: SELECT DISTINCT p. "product_name" FROM "products" AS p

 $select = $db->select() ->distinct() ->from(array('p' => 'products'), 'product_name'); 
0
source

All Articles