我有一列“小时” 我有一列“种类”(可以是 1,2 或 3)
我想做这样的事情:
SELECT count(id), SUM(hour) as totHour, SUM( IF ( kind = 1, 1, 0 ) ) as countKindOne
或
SELECT count(id), SUM(hour) as totHour, COUNT( IF ( kind = 1 ) ) as countKindOne
但是 mysql 告诉我我有一个错误...错误是什么!?
请参阅此 stackoverflow 主题:MySQL SUM IF field b = field a
..我无法回复这个...
最佳答案
您可以使用 CASE
语句:
SELECT count(id),
SUM(hour) as totHour,
SUM(case when kind = 1 then 1 else 0 end) as countKindOne
关于MySql:是否可以 'SUM IF' 或 'COUNT IF' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075505/