Query `count` returning the wrong value
Hi everyone! I'm hoping someone can help me understand the
This returns 2 records, which is correct, however, the
count()
function better. My goal is to retrieve the count of all records in the join. I have the following query:
This returns 2 records, which is correct, however, the
count
value is 1. I think something is wrong with my groupBy clause but not sure what. I was getting errors until I add that groupBy clause and the data returned is correct, just the count is wrong. The relationship between users and organizationMemberships is one-to-many. Please let me know if there are any other details I need to share2 Replies
Your group by should look like
I'm not sure about the count.
That's not how count in SQL works in a GROUP BY query. It counts the number of records that match the group by criteria.
For example, if you had a table with a
category_id
, and you grouped it by that column, the value of count in the resulting dataset would be the number of records matching each category_id
in the table.
So:
Then SELECT COUNT(*) FROM table GROUP BY category_id
Instead, you can get the total number of records returned like this:
selectResult.length