gpt4 book ai didi

mysql - 如何使用 MySQL 有条件地处理被零除

转载 作者:IT老高 更新时间:2023-10-28 23:50:45 26 4
gpt4 key购买 nike

在 MySQL 中,这个查询可能会抛出被零除的错误:

SELECT ROUND(noOfBoys / noOfGirls) AS ration
FROM student;

如果 noOfGirls0 则计算失败。

处理此问题的最佳方法是什么?

我想有条件地将 noOfGirls 的值更改为 1,当它等于 0 时。

有没有更好的办法?

最佳答案

是的,你可以做一个案例:

select case when noOfGirls=0 then noOfBoys 
else round(noOfBoys/noOfGirls) end as ration
from student;

但你可能想要:

select case when noOfGirls=0 then 1 
else round(noOfBoys/noOfGirls) end as ration
from student;

关于mysql - 如何使用 MySQL 有条件地处理被零除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8245438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com