Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql group by and count rows problem

Tags:

sql

mysql

let's say mysql is something like this

select x,y 
from xx 
group by y

i want to know how many rows that select will get, i tried to use count but it will n't return all results since i'm using group by.

how to do that?

Thanks

like image 557
trrrrrrm Avatar asked Apr 13 '10 16:04

trrrrrrm


People also ask

How do I count the number of rows in a MySQL GROUP BY?

In MySQL, the COUNT() function calculates the number of results from a table when executing a SELECT statement. It does not contain NULL values. The function returns a BIGINT value. It can count all the matched rows or only rows that match the specified conditions.

Can we use GROUP BY with count?

The GROUP BY statement is often used with aggregate functions ( COUNT() , MAX() , MIN() , SUM() , AVG() ) to group the result-set by one or more columns.

How do I count rows in MySQL?

The following are the syntax to get the row count of a single table in MySQL: SELECT COUNT (*) FROM table_name.

How count works with GROUP BY in SQL?

SQL – count() with Group By clause The count() function is an aggregate function use to find the count of the rows that satisfy the fixed conditions. The count() function with the GROUP BY clause is used to count the data which were grouped on a particular attribute of the table.


1 Answers

You can wrap your query like so:

SELECT COUNT(*) FROM
  (select x,y  
    from xx  
    group by y) sub;
like image 156
dnagirl Avatar answered Sep 22 '22 07:09

dnagirl