Show occurrences as fields

Asked

Viewed 23 times

1

I am doing a query in the database, I wonder if it is possible to group elements and display them as if they were fields and within these fields the amount 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 consultation would look like this:

pendente| concluida | 
-----------------------------------------
2       | 1         | 

1 answer

4


Basically that’s 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

  • 1

    God, I was killing myself here to remember some command that did that, I would never know that was it, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.