我有简单的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
最佳答案
查询是可能的,只是语法错误,应该是:
SELECT
SUM(IF(designation LIKE "Senior%", 1,0)) AS `Senior_total`,
SUM(IF(designation LIKE "Junior%", 1,0)) AS `Junior_total`
From mytable
关于php - mysql每个指定字数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25075378/