Count MySQL occurrences

0

This would be my table called numeros with its columns called N1 N2 N3 N4 and their respective numeros in each column.

What I can not do is to tell me how many times each numero came out in each column.

EXAMPLE TABLE:

    NUMEROS
---------------
N1   --     N2

1    --     22     
5    --     1  
1    --     12     

Given the case of the table I put above as an example, the query should show this result.

   N1                                                              
---------
Numero - Veces que salio  
   1   --    2
   5   --    1


   N2   
---------
Numero - Veces que salio
    22     --   1
    1      --   1
    12     --   1

The query I'm using is this one, but it does not give me the results as I want:

SELECT uno,dos, COUNT(uno) as CantRep1,
COUNT(dos) as CantRep2
FROM jugada
GROUP BY uno,dos

uno and dos would be in the example n1 and n2 .

    
asked by Nicolas 30.09.2017 в 12:05
source

1 answer

0

From what I understand, it would be the query you need:

Select 'N1', N1, count(*) as cnt from numeros group by 1,2
 union 
( Select 'N2', N2, count(*) as cnt from numeros group by 1,2 )
 union 
( Select 'N3', N3, count(*) as cnt from numeros group by 1,2 )
 union 
( Select 'N4', N4, count(*) as cnt from numeros group by 1,2 )
    
answered by 27.09.2018 в 01:15