I want to write a single Postgres SQL statement that says look for a user with color X and brightness Y. If that user exists, return all of its row data. If not, create a new
with sel as (
select color, brightness, size, age
from mytable
where color = 'X' and brightness = 'Y'
), ins as (
insert into mytable (color, brightness, size, age)
select 'X', 'Y', 6.2, 40
where not exists (
select 1 from sel
)
returning color, brightness, size, age
)
select color, brightness, size, age
from ins
union
select color, brightness, size, age
from sel