List and count results that repeat within an array

Asked

Viewed 19 times

-2

I have the following table

ID|  nome                      | Tipo
1 | Teste, João, Maria         | Indicação
2 | Teste, Maria, José         | Projeto de Lei
3 | Maria, José, João, Teste   | Indicação 
4 | Maria, João, José          | Denúncia

I needed to list the types and count the amount of each of them that has a given name, ex:

Name: Test

  • Indication (2)
  • Draft Law (1)
  • Complaint (0)

I built the code below but it didn’t work:

SELECT   tipo,
         COUNT(tipo) AS Qtd
FROM  pessoas
WHERE FIND_IN_SET ("teste", nome)
ORDER BY COUNT(tipo) DESC

Take the example in practice: http://sqlfiddle.com/#! 9/0b41e8/1

1 answer

0


I don’t know if the question as I understand it is the answer as you hope, what I did based on understanding was:

inserir a descrição da imagem aqui

Thus:

SELECT *,COUNT(tipo) AS Qtd
FROM pessoas
WHERE FIND_IN_SET ("teste", nome)
GROUP BY tipo
  • That’s right, but what if I wanted to list also the guys who don’t have any amount?

  • You change the condition of your WHERE, because you specified the condition of the results, right?

  • 1

    Check, resulting when there is no null, considering empty http://sqlfiddle.com/#! 9/d02feb2/1

  • 1

    I think I’ll be able to adapt to what I need. Thanks for the help Eliseu

Browser other questions tagged

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