Show occurrences as fields

1

I am making a query in the database, I would like to know if you can group elements and display them as if they were fields and within those fields the number of occurrences.

ex: The table looks like this:

nome_tarefa | data_criacao | status
-----------------------------------------
tarefa 1    | 12-02-2016   | pendente
tarefa 2    | 13-02-2016   | pendente
tarefa 3    | 13-02-2016   | concluida

After the query it would look like this:

pendente| concluida | 
-----------------------------------------
2       | 1         | 
    
asked by anonymous 08.03.2016 / 08:23

1 answer

4

Basically this is it:

SELECT
   SUM( IF( status='pendente' , 1, 0 ) ) AS pendente,
   SUM( IF( status='concluida', 1, 0 ) ) AS concluida
FROM
   tarefas

See working on SQL Fiddle

    
08.03.2016 / 08:38