COUNT(*) returning multiple rows instead of just one

后端 未结 4 1702
春和景丽
春和景丽 2020-12-16 23:19

Why does COUNT() return multiple rows when I just need the total count of how many rows my query generates?

Should return 1078.

相关标签:
4条回答
  • 2020-12-16 23:51

    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.

    0 讨论(0)
  • 2020-12-16 23:53

    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`
    
    0 讨论(0)
  • 2020-12-16 23:54

    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()
        )
    
    0 讨论(0)
  • 2020-12-17 00:06

    Do not use group by, it will count the number of each group.

    0 讨论(0)
提交回复
热议问题