MYSQL按组显示总和和状态

时间:2019-06-21 18:12:42

标签: mysql

使用MySQL如何运行查询,该查询将返回状态和总和并将结果分组。

我有这样的桌子...

 ==========================================
 id | state       | fieldKey
 ==========================================
 1  | Florida     | property_state
 2  | Idaho       | property_state
 3  | Florida     | property_state
 4  | Ohio        | property_state
 5  | Oregon      | property_state
 6  | California  | property_state
 7  | California  | property_state
 8  | California  | property_state

这是我无法执行的查询:

 select sum(id), state from ProjectFieldValue where fieldKey = 'property_state' group by fieldKey

这是我要输出的内容:

 2, Florida
 1, Idaho
 1, Ohio
 1, Oregon
 3, California

谢谢!

1 个答案:

答案 0 :(得分:0)

您不想求和。您只想计数(更新)

SELECT COUNT(id) as Number, state 
FROM ProjectFieldValue 
WHERE fieldKey = 'property_state' 
GROUP BY state