November 14 2014

Getting the number of rows with a GROUP BY query in MySQL

Tagged Under :

MySQL
If you use the COUNT() function with GROUP BY. It will count the number of rows within each group, not the total number of rows. If you want count the number of rows with the groups, you are not allow use GROUP BY inside the query.

Instead of use GROUP BY you may use DISTINCT inside the query. The DISTINCT function similar with GROUP BY, but it can put inside the COUNT() function.

SELECT COUNT( DISTINCT(id) ) FROM table

Make a Comment

*

code