Scenario:
I have a table named snippets with 5 rows,
I have a table named revisions with 15 rows
What I wanted to do was get all snippets, which belongs a user_id on revisions, which includes the snippet_id.
I have the following mysql query:
SELECT DISTINCT * FROM "snippets"
INNER JOIN "revisions"
ON "user_id" = "1"
AND "snippet_id" = "snippets.id"
GROUP BY "snippets"."id"
ORDER BY "created_at"
That should display all snippets where revisions.user_id = 1. The problem is, why is my count different? The total results from that query is 5, but when I do:
SELECT COUNT(distinct snippets.id) FROM "snippets"
INNER JOIN "revisions"
ON "user_id" = "1"
AND "snippet_id" = "snippets.id"
GROUP BY "snippets"."id";
The result is 1 1 1 1 1
There is no meaning for
DISTINCT COUNT(*). May be you are looking forCOUNT(DISTINCT someotherfield)instead of it. SinceCOUNT(*)includes any rows that hasNULLvalues.Update:* Don’t
COUNTthe same field that you used in theGROUP BYclause. That is why you are getting the result1 1 1 1 1. In this case remove theGROUP BY "snippets"."id"clause.