How to count total of multiple type records in a single query in sql

0

Hello Today I'm doing the registration counts this way

$sql1 = "SELECT COUNT(*) AS total1 FROM a_finan Where  cat = 'Ativos' ;
$resultado1 = mysql_query($sql1) or die ("Erro na consulta1");
$linha1 = mysql_fetch_assoc($resultado1);
echo $total1 = $linha1['total1'];

$sql2 = "SELECT COUNT(*) AS total2 FROM a_finan Where  cat = 'Inativos' ;
$resultado2 = mysql_query($sql2) or die ("Erro na consulta2");
$linha2 = mysql_fetch_assoc($resultado2);
echo  $total2 = $linha2['total2'];

1st Total active registration account the 2nd total count of inactive records and 3rd is still the same ...

I would have some way to do a single query count so the code does not look huge

    
asked by anonymous 16.04.2015 / 16:44

1 answer

2
SELECT cat, COUNT(*) as total
FROM a_finan
GROUP BY cat

This will bring the total of records grouped by category ' cat '.

    
16.04.2015 / 16:51