Count mysqli fields

1

**** I have a table called places and the field I want to count is called state the values that can have states is 0 = processed, 1 = accepted, 2 = rejected I want you to print them like that, I already did count but I get row by row and don sum if it would appear as the image but I do not understand how ****

This is the query you try but the sum does not count

SELECT sum(IF(estado=0,1,2)) as "tramite", sum(IF(estado=1,2,0)) 
as "Aceptado", sum(IF(estado=2,0,1)) as "Rechazado" FROM lugares
    
asked by JV93 17.10.2018 в 03:01
source

1 answer

1

You can do it with a SUM(CASE ... WHEN) , for example:

SELECT
       SUM(CASE WHEN estado = 0 THEN 1 ELSE 0 END) Tramite,
       SUM(CASE WHEN estado = 1 THEN 1 ELSE 0 END) Aceptado,
       SUM(CASE WHEN estado = 2 THEN 1 ELSE 0 END) Rechazado       
FROM lugares;

What you will do is add 1 when each condition is met.

    
answered by 17.10.2018 / 03:46
source