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

使用的 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);
经验分享 程序员 微信小程序 职场和发展