I have this query:
SELECT sum((a.cant*b.cost)/b.cant) AS sum1
FROM
(SELECT partNo,cant
FROM table1 WHERE id=1) AS a,
(SELECT color,cost,cant
FROM table2 WHERE color in
(SELECT partNo FROM table1 WHERE id=1)) AS b
WHERE a.partNo=b.color
The problem is that sometimes b.cant will return 0, and when I do this part: SELECT sum((a.cant*b.cost)/b.cant) AS sum1 it will return a divide by zero error message, I was thinking on doing something like this:
SELECT IF (b.cant=0,0,sum((a.cant*b.cost)/b.cant)) AS sum1
Meaning that if b.cant equals zero then I want to return a 0 as final result and stop doing the division, but if it is not equal 0 then I will do the division, in my head this was the solution but is giving me an error.
Is there any other way to avoid doing the division and just return 0 if the divisor is 0?