Mysql数据库,巧妙使用IFNULL()函数,累计求和.

本文通过一个SQL示例展示了IFNULL()函数在处理NULL值时的作用,解释了当字段值为NULL时如何避免计算结果也为NULL。在SQL查询中,对包含NULL值的字段进行运算可能导致整体结果为NULL,而IFNULL()函数可以将NULL值替换为指定的默认值,确保计算的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      使用的 student 学生表及表数据如下所示:

-- 学生表
CREATE TABLE `student` (
  `st_id` varchar(64) NOT NULL DEFAULT '' COMMENT '编号',
  `st_name` varchar(255) DEFAULT '' COMMENT '姓名',
  `st_age` int(3) DEFAULT NULL COMMENT '年龄',
  `st_address` varchar(255) DEFAULT NULL COMMENT '地址',
  `st_sex` varchar(8) DEFAULT NULL COMMENT '性别',
  `st_status` int(2) DEFAULT NULL COMMENT '状态, 0-正常, 1-异常. ',
  `st_money` decimal(14,2) DEFAULT NULL COMMENT '生活费',
  `create_date` datetime DEFAULT NULL COMMENT '创建时间',
  `update_date` datetime DEFAULT NULL COMMENT '修改时间',
  PRIMARY KEY (`st_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生表';


-- 表数据
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('001', '侯耀华', 77, '北京市-海淀区', '男', NULL, NULL, '2019-06-10 10:56:21', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('002', '郭德纲', 45, '河北省-保定市', '男', 0, NULL, '2019-06-09 16:56:21', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('003', '曹云金', 33, '河南省-洛阳市', '男', -5, NULL, '2019-06-10 13:22:21', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('004', '岳云鹏', 31, '河南省-周口市', '男', 0, NULL, '2019-06-08 09:35:27', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('005', '宋丹丹', 52, '黑龙江-佳木斯', '女', 0, NULL, '2019-06-09 07:35:27', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('006', '倪妮', 31, '江苏省-南京市', '女', -5, 450.00, '2019-06-08 04:16:41', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('007', '江流儿', 445, '明朝-汴梁人氏', '男', 0, 300.00, '1798-06-11 15:11:47', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('008', '赵丽颖', 31, '河南省-平顶山市', '女', 0, NULL, '2019-06-06 03:24:51', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('009', '李云鹤', 33, '河北省-怀柔', '男', 2, 1.00, NULL, NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('010', '李晓雅', 27, '广西省-桂林市', '女', 1, NULL, NULL, NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('user_002', '吴玉', 19, '福建省-龙岩市', '女', 1, 0.50, '2019-06-08 10:14:02', NULL);
INSERT INTO student (st_id, st_name, st_age, st_address, st_sex, st_status, st_money, create_date, update_date) VALUES ('user_003', '上官飞', 20, '河北省-廊坊市', '男', NULL, NULL, '2019-06-07 10:14:06', NULL);

                     

以下的SQL是用来讲解 IFNULL() 函数的作用,SQL并没有实际上的业务逻辑意义。

在下面SQL中,我们可以看到,SUM(s.st_money) 为NULL,进而导致 " SUM(s.st_money) + SUM(s.st_age) AS temp1 " 也为NULL, 当我们使用 IFNULL() 函数进行处理后,可以看到 temp3 字段的值达到预期。

SELECT 
	SUM(s.st_money) AS '生活费累加',
	SUM(s.st_age) AS '年龄累加',
	SUM(s.st_money) + SUM(s.st_age) AS temp1,
	NULL + SUM(s.st_age) AS temp2,
	(IFNULL(SUM(s.st_money), 0) + IFNULL(SUM(s.st_age), 0)) AS temp3
	
FROM student s
WHERE s.st_id IN ('001', '002', '003', '004', '005');


              

                              

参考文章: Mysql 两字段相加的值                

                                 

                                                    

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值