Count database records without repeating

Asked

Viewed 46 times

-1

I have a table that has several records with the same information, example:

id |  nome      |  profissao
1     Carlos       Pedreiro
2     Jean         Garçon
3     Victor       Pedreiro
4     Ana Paula    Secretaria
5     Paula        Secretaria
6     Karina       Balconista

What I need to count is the amount of profession but without repeating, in the example I gave, has 6 records, but only has 4 different professions, this is what I need to count, I do not know how to count but without repeating

1 answer

2


Do it this way:

SELECT
   t.profissao,
   COUNT(t.profissao) as total
FROM
   tabela t
GROUP BY
   t.profissao

In the SELECT place COUNT(t.profissao), grouping by the same field, ie, GROUP BY t.profissao. This way he will return the number of repetitions of each profession, and to know which profession belongs to the quantity shown, just add the field profissao, as shown in query above.

The result of the query would be more or less this:

total     |  profissao
2            Pedreiro
1            Garçon
2            Secretaria
1            Balconista
  • After that just give a rowCount()?

  • The way the SELECT, it will return a query with two fields. A call profissao with the name of the profession and another field called total, showing the amount of the same.

  • It worked fine, thank you!

Browser other questions tagged

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