简体   繁体   中英

mysql count word per designation

I have simple mysql table:

id  designation   
1   Senior1      
2   Senior1      
3   Senior2    
4   Junior1      
5   Junior2      

What I'm trying to count the designation.

I have a sample query but it only counts the specific word.

SELECT

SUM(IF(designation   = "Senior1", 1,0)) AS `Senior_total`,
SUM(IF(designation   = "Junior1", 1,0)) AS `Junior_total`

From mytable

as a result, I have.

Senior_total   Junior_total
2               1

I was looking for the output would be like this.

Senior_total   Junior_total
3               2

I was thinking if this query is posible. but I think its not.

SELECT

SUM(IF(designation   = "Senior%", 1,0)) AS `Senior_total`,
SUM(IF(designation   = "Junior%", 1,0)) AS `Junior_total`

From mytable

The query is possible it is just you syntax that is wrong it should be:

SELECT

SUM(IF(designation   LIKE "Senior%", 1,0)) AS `Senior_total`,
SUM(IF(designation   LIKE "Junior%", 1,0)) AS `Junior_total`

From mytable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM