ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 7.2、sum l Sum可以取得某一个列的和,null会被忽略 l 取得薪水的合计 ``` select sum(sal) from emp; ``` ![](https://img.kancloud.cn/cc/99/cc992121dd2b3f5b2f27b09c49856bd4_407x131.png) l 取得津贴的合计 ``` select sum(comm) from emp; ``` ![](https://img.kancloud.cn/2c/da/2cda55df1f339076025217fd4062ca4d_392x128.png) null会被忽略 l 取得薪水的合计(sal+comm) ``` select sum(sal+comm) from emp; ``` ![](https://img.kancloud.cn/f4/7e/f47e16351feabf39cd1b358e485ce92c_375x129.png) 从以上结果来看,不正确,原因在于comm字段有null值,所以无法计算,sum会忽略掉,正确的做法是将comm字段转换成0 ``` select sum(sal+IFNULL(comm, 0)) from emp; ``` ![](https://img.kancloud.cn/ee/e2/eee2ed51f8443541acb3110a28547439_453x129.png)