如何从视图的列为IFNULL(1/0,0)的视图中插入表?

时间:2018-11-17 12:46:07

标签: mysql

我的环境:

MariaDB 10.3.10

我创建一个表pe:

CREATE TABLE pe (
  id int(11) NOT NULL AUTO_INCREMENT,
  weight int(11) DEFAULT NULL,
  height int(11) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

id, weight, height
1, 63, 172
2, 62, 173
3, null, null
4, 60, null

并创建一个视图pe_view:

CREATE VIEW pe_view AS
SELECT
weight, height, IFNULL(FORMAT(weight/height/height*10000, 2), 0) AS bmi
FROM pe;

然后创建一个表pe_per_year:

CREATE TABLE pe_per_year (
  id int(11) NOT NULL AUTO_INCREMENT,
  weight int(11) DEFAULT NULL,
  height int(11) DEFAULT NULL,
  bmi double(5,2) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

现在,我想从pe_view插入pe_per_year。我使用:

INSERT INTO
pe_per_year(weight, height, bmi)
SELECT weight, height, bmi
FROM pe_view;

它告诉我“被零除”。请问我怎么了?以及如何将一些IFNULL(1/0,0)数据插入double(5,2)中?

0 个答案:

没有答案