How to count single column in mysql?

eg:

with select query I got the result below:

 columnA columnB type1 typea type2 typea type3 typeb type4 typec type5 typed type6 typed type7 typed 

with DISTINCT I only got a distinct result , but I also want to get the total number for each individual.

and now I want to get the total number

 typea,typeb,typec and typed. 

As well as:

 columnB total typea 2 typeb 1 typec 1 typed 3 

Thank you very much!

+4
source share
1 answer

You can use GROUP BY to get results by type:

 SELECT columnB, COUNT(*) AS 'total' FROM myTable GROUP BY columnB; 

This will give you exactly the result you are looking for.

MySQL Documentation: 11.16.1 GROUP BY (Aggregate) Functions

+9
source

Source: https://habr.com/ru/post/1342014/


All Articles