COALESCE statement rescued sum of sql problem

mysql + mybatis have a sql statement is statistical purposes

<select id="getNum" resultType="map">

The result is a map, the result of the select statement is that some sum

select sum(t.anum),

          sum(t.bnum)

from tableSum t

where t.id = #{id}

Debugging found that the database record clearly have fallen, but check out the object is null,

Numerical problems the sum of the fixed portion written does not exist, it should be the sum of the parts.

Solutions with COALESCE function

 

The COALESCE ( the SUM (t.anum), 0) the AS Anum 
into this, initially 0, it will always have a value, it is not null.

 

Guess you like

Origin www.cnblogs.com/zhangcheng1/p/11491327.html