Abel Pule Chilungu
Abel Pule Chilungu

Reputation: 33

counting COUNT(*) results in MySQL

I need to count the number of records returned from a MySQL query.

SELECT 
    COUNT(sa.facility_hmis) AS deployed
FROM
    facility_service_area sa
GROUP BY sa.facility_hmis 

this will give me lets say 6 rows as the result, but i need it to give me a single row with just the number counted rows i.e 6

Upvotes: 0

Views: 40

Answers (3)

Nir Levy
Nir Levy

Reputation: 12953

Use distinct

SELECT COUNT(distinct facility_hmis) AS deployed FROM facility_service_area

Upvotes: 3

zerkms
zerkms

Reputation: 254926

If all you need is to count number of distinct facility_hmis you can do that explicitly, without counting of anything else:

SELECT COUNT(DISTINCT facility_hmis) FROM facility_service_area

Upvotes: 3

Tah
Tah

Reputation: 1536

Wrap the query in another count()

SELECT COUNT(1) from (
  SELECT COUNT(sa.facility_hmis) AS deployed FROM facility_service_area sa GROUP BY sa.facility_hmis
)

Upvotes: 1

Related Questions