How to create rule in MYSQL to divide the value of one column by another?

0

I want to know the average values (value2) in a table using AVG () (or another form).

I have a table in MYSQL with the columns id, total_value, valuem2 (Value per m²), and area.

In some lines the value m2 is empty and in others the total value is empty.

Is there any way to do a select if the field is empty, does it make an account? Type the below:

tabela_terrenos
id|valortotal|valorm2|area
1 |100000    | 1000  |100
2 |150000    |       |100
3 |          | 1500  |200

SE valorm2 = vazio, então valorm2 = valortotal / area
SENÃO valorm2 = valorm2

SELECT AVG(valorm2)
FROM tabela_terrenos
    
asked by anonymous 07.08.2018 / 19:59

1 answer

2

Try this:

SELECT IFNULL(AVG(valorm2), valortotal/area) valorm2
FROM tabela_terrenos
    
07.08.2018 / 20:03