Search code examples
mysqlsqlpropel

Filtering out duplicates then getting the total number of records with Propel


I have a MySQL database which I am using Propel with. I am totally new to Propel so my syntax isn't great. What I want to do is find the total amount of records in a column, filtering out duplicates from the results.

I can get the total amount of records easily enough using

$distinctCount = TblUserAnswersQuery::create()
->setDistinct('User_Id')
->find()
->count();

But this just gives me the total amount of records. As you can see I've tried using setDistinct() but this still just returns the total amount of records without filtering out duplicates.

To do this is SQL, I could use this:

SELECT COUNT(distinct User_Id) AS distinctCount FROM Tbl_User_Answers

I'm not getting any errors as as far as Propel knows I'm getting what I want but clearly I'm not because my syntax is wrong. What I really need is to know how to convert the SQL above into Propel syntax.


Solution

  • You can must specify the distinct column, then add distinct, you also don't need find :

    $distinctCount = TblUserAnswersQuery::create()
      ->select(['User_Id'])
      ->distinct()
      ->count();