每个指定的mysql计数字

时间:2014-08-01 07:48:51

标签: php mysql sql

我有简单的mysql表:

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

我试图计算这个名称。

我有一个示例查询,但它只计算特定的单词。

SELECT

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

From mytable
结果,我有。

Senior_total   Junior_total
2               1

我一直在寻找输出就是这样。

Senior_total   Junior_total
3               2

我在想这个查询是否可行。但我认为不是。

SELECT

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

From mytable

1 个答案:

答案 0 :(得分:0)

查询是可能的只是你的语法应该是错误的:

SELECT

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

From mytable