Irma Elita
Irma Elita

Reputation: 249

How to count rows using group by

I have a table like this.

---------
| block |
---------
| A1    |
| A1    |
| A2    |
| A3    |
---------

I want to count found rows using group by, so it would return value like this.

--------------
| total_block|
--------------
| 3          |
--------------

What is the correct query to show results like that? I have already tried using SQL_CALC_FOUND_ROWS but it return values = A1, A2, A3. Thank you.

Upvotes: 0

Views: 59

Answers (2)

sumitya
sumitya

Reputation: 2691

Not the very nice approach , but if it has to be done via group by then, you can try this.

select count(cnt) from (select count(1) as cnt  from table_inner group by block) table_outer;

Upvotes: 0

jarlh
jarlh

Reputation: 44766

Looks like you just want the number of different blocks:

select count(distinct block) as total_block from tablename

Upvotes: 3

Related Questions