Search code examples
mysqlsqlcountunique

Count total item types and number in a column in MYSQL


Let's say I have a column with values like types

CPU
CPU
CPU
VDU
VDU

I want to count the number of times an item like CPU is in that column.

My query

select COUNT( * ) as total from `table` where `types` like "%CPU%"

But here I know that there is an item named cpu. Can I write a query that gives me the number of times a value occurs. Like the query should return CPU:3 VDU:2......


Solution

  • According to P.Salmon's comment, You can use GroupBy to achieve it, it seems to look like below

    select COUNT(*) as total, `types`  
    from `table` 
    where `types` like "%_your_filter_condition_here_%"
    group by `types`