SQL 当某个值为 NULL 时触发总和行数
想对 3 行求和,但是当 value1 为 NULL 时,想跳过它以对所有其他行求和:
CREATE TRIGGER `sum_total_test_before_insert` BEFORE INSERT ON `test`
FOR EACH ROW
BEGIN
IF (NEW.value1 IS NULL) THEN
SET NEW.Total = (NEW.value2 + NEW.value3);
ELSE
SET NEW.Total = (NEW.value1 + NEW.value2 + NEW.value3);
END IF;
Total 行上的预期值示例
value1 value2 value3 Total
5 6 6 17
2 5 4 11
NULL 5 4 9
错误:#1064 - 第 5 行 '' 附近的语法出错