首页 > 代码库 > mybatic 结果为null,返回0

mybatic 结果为null,返回0

 

返回空容易产生异常,当值为null时,返回为0的写法

<select id="getBookWordCountBookPssAndChapterLockOrPass" resultType="LONG" parameterType="STRING">
        select  COALESCE(sum(word_count),0) as word_count
        from t_news_count 
        WHERE 
        chapter_id  in 
        ( SELECT chapter_id 
          from t_content_chapter  
          WHERE 
          book_id = #{book_id,jdbcType=VARCHAR} 
          AND  
          ( status = 3 OR status = 1) ) 
        AND 
        ( 3 = (SELECT current_state 
               FROM t_content_metadata 
               WHERE  book_id = #{book_id,jdbcType=VARCHAR} ) 
        ) 
    </select>

 

mybatic 结果为null,返回0