Esteban Cazorla Esteban Cazorla - 4 months ago 13
MySQL Question

MySQL column with conditional value

I have this basic SQL sentence:

SELECT p.s1,pr.s1 FROM `prop` p LEFT OUTER JOIN prom pr ON pr.id = p.prom


My intention is to have a column with the value of
pr.s1
if it's not null, and
p.s1
in other case. It's very simple, but i don't know how to do it correctly.

Any idea will be very appreciated, thanks.

Answer

You can use IFNULL:

SELECT IFNULL(pr.s1, p.s1) AS s1
FROM `prop` p
LEFT OUTER JOIN prom pr
    ON pr.id = p.prom
Comments