How to conditionally handle division by zero with MySQL

前端 未结 7 1036
春和景丽
春和景丽 2020-12-28 16:01

In MySQL, this query might throw a division by zero error:

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

If noOfGirls

7条回答
  •  天命终不由人
    2020-12-28 16:52

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

提交回复
热议问题