mysql如何COUNT()nullleble字段+ IFNULL

时间:2016-02-11 12:47:46

标签: mysql sql

字段版本可以为null但我也需要对这些行进行计数 我试图这样做:IFNULL(COUNT(版本),1),但不工作

select MIN(id) as id, version, IFNULL(COUNT(version), 1) as total
from hlrlookup.hlrlookup
WHERE create_timestamp > '2015-02-01 05:00:00' and create_timestamp < '2016-04-28 06:00:00'
group by version;

帮助

2 个答案:

答案 0 :(得分:2)

alpha:0.7永远不会返回COUNT() - 而是返回NULL。如果要计算所有行数,请使用0

COUNT(*)

编辑:

我应该注意,表达你想要做的逻辑的正确方法是:

select MIN(id) as id, version, COUNT(*) as total
from hlrlookup.hlrlookup
WHERE create_timestamp > '2015-02-01 05:00:00' and
      create_timestamp < '2016-04-28 06:00:00'
GROUP BY version;

答案 1 :(得分:0)

您在分组声明中有version。您只需对每个版本的行进行求和:

select MIN(id) as id, version, SUM(1) as total
from hlrlookup.hlrlookup
WHERE create_timestamp > '2015-02-01 05:00:00' and create_timestamp < '2016-04-28 06:00:00'
group by version;