I have a query that looks like such:
SELECT COUNT(A.Value1) AS Count, B.id AS Id1, C.id AS Id2
FROM Table1 A LEFT JOIN Table2 B ON (B.Name LIKE '%UserInput1%')
LEFT JOIN Table3 C ON (C.Name LIKE '%UserInput2%')
WHERE A.Value1 LIKE 'CertainValue'
The query runs perfectly fine; it’s just that the COUNT I get from Table1 does not give me the actual count of the number of Value1s in Table1, but rather the number of Value1s multiplied by the number of tables that are actually joined.
Is there any way I can obtain the COUNT from just Table1 that fits into the criteria of A.Value1 LIKE 'CertainValue', while joining the other tables?
FYI: I’m using MySQL!
The problem is that you’re doing the counting after joining, so you’re counting the number of A.Value1’s in the resulting cross-product. If you want the count from the original table, you should use a sub-query. I don’t think this will cause much of a performance problem, it should actually improve it, since it reduces the size of the resulting join.