Why does COUNT() return multiple rows when I just need the total count of how many rows my query generates?
Should return 1078.
Well, simple answer. Don't GROUP BY
if you don't need groups.
Either use COUNT(DISTINCT articles.company)
without GROUP BY
or keep the the GROUP BY
and wrap the whole query in a SELECT COUNT(*) FROM (...) AS data
, if you want to count the groups.
The COUNT()
is working as expected. When you put a group by
clause, the count()
gives you the result for GROUP BY
. If you wish to get the count of rows in a query that includes group by
, use it as a subquery instead.
Something like:
SELECT COUNT(*) FROM (SELECT * FROM `table`
GROUP BY `column1`) AS `a`
Write the above query as subquery then it will give proper result and dont use group by
select count(*) from (select articles.id from 'contract_prices' left join 'articles' on (arcticles.id = contract_prices.article) where 'contract_to' >= curdate() )
Do not use group by, it will count the number of each group.