2017-08-16 104 views
-2

我想從查詢生成插入到表1365分工,但這種查詢可以通過零錯誤拋出師:如何解決錯誤:0在MySQL

insert into rpp 
select IFNULL(IF((IF(((((((`s`.`smi` + `s`.`jmb`) + `s`.`ruah`) + 
`s`.`stg_jd`) + `s`.`jdw_pro`)/`p`.`rate`) <= 2), 
((`p`.`rate` * 4) - ((((`s`.`smi` + `s`.`jmb`) + `s`.`ruah`) 
+ `s`.`stg_jd`) + `s`.`jdw_pro`)),0) > 0), 
(FLOOR((IF(((((((`s`.`smi` + `s`.`jmb`) + `s`.`ruah`) 
+ `s`.`stg_jd`) + `s`.`jdw_pro`)/`p`.`rate`) <= 2), 
((`p`.`rate` * 4) - ((((`s`.`smi` + `s`.`jmb`) + 
`s`.`ruah`) + `s`.`stg_jd`) + `s`.`jdw_pro`)), 
0)/`p`.`bets`)) * `p`.`bets`),0),0) AS `rp` 
FROM (`produk` `p`LEFT JOIN `stok` `s` ON ((`p`.`kode` = `s`.`kode_produk`))) 

提前感謝!

+0

添加在分母中檢測零的邏輯,在這種情況下插入一些其他值。順便說一句,您的查詢是難以辨認的。 –

+2

上帝,這是我見過的最醜陋的查詢。你真的認爲有人會嘗試理解這一點? – sagi

+0

Iam Sory我的查詢最醜。簡單地說:插入到選擇ifnull(1/0,0) –

回答

0

有多種方法可以解決這個問題。

添加邏輯,你除以1或你想要處理它。爲此,有這個奇妙的功能IF(),它的工作原理類似於IF(condition, then, else),例如IF(my_column = 0, 1, my_column)

另一種方法是相應地設置你的sql_mode。看看sql_mode error_for_division_by_zero

The ERROR_FOR_DIVISION_BY_ZERO mode affects handling of division by zero, which includes MOD(N,0). For data-change operations (INSERT, UPDATE), its effect also depends on whether strict SQL mode is enabled.

If this mode is not enabled, division by zero inserts NULL and produces no warning.

If this mode is enabled, division by zero inserts NULL and produces a warning.

If this mode and strict mode are enabled, division by zero produces an error, unless IGNORE is given as well. For INSERT IGNORE and UPDATE IGNORE, division by zero inserts NULL and produces a warning.

For SELECT, division by zero returns NULL. Enabling ERROR_FOR_DIVISION_BY_ZERO causes a warning to be produced as well, regardless of whether strict mode is enabled.

As of MySQL 5.7.4, ERROR_FOR_DIVISION_BY_ZERO is deprecated. In MySQL 5.7.4 through 5.7.7, ERROR_FOR_DIVISION_BY_ZERO does nothing when named explicitly. Instead, its effect is included in the effects of strict SQL mode. In MySQL 5.7.8 and later, ERROR_FOR_DIVISION_BY_ZERO does have an effect when named explicitly and is not part of strict mode, as before MySQL 5.7.4. However, it should be used in conjunction with strict mode and is enabled by default. A warning occurs if ERROR_FOR_DIVISION_BY_ZERO is enabled without also enabling strict mode or vice versa. For additional discussion, see SQL Mode Changes in MySQL 5.7.

Because ERROR_FOR_DIVISION_BY_ZERO is deprecated, it will be removed in a future MySQL release as a separate mode name and its effect included in the effects of strict SQL mode.

+0

好吧謝謝你,添加忽略已解決 –