Can you create a rule in MYSQL to divide the value of one column by another?

Asked

Viewed 271 times

0

I want to know the average of the values (value m2) in a table using AVG() (or otherwise).

I have a table in MYSQL with the columns id, total value (Total value), value m2 (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 make a select for if the field is empty, it makes an account? Type the below:

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

then I’d need it before I brought the average, something like:

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

SELECT AVG(valorm2)
FROM tabela_terrenos
  • the answer below solved the problem?!

1 answer

2


Try this:

SELECT IFNULL(AVG(valorm2), valortotal/area) valorm2
FROM tabela_terrenos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.