0

I want to insert a row if it doesn't exist, else return the existing row. The following works fine for new rows, but it returns nothing in case the row already exist.

INSERT INTO product (brand, type)
SELECT 'aa', 'bb'
WHERE
    NOT EXISTS (
        SELECT * FROM product WHERE brand='aa' AND type='bb'
    )
RETURNING *

I know a similar question had been asked before here, but the proposed solution (as I show above) doesn't work for me. In other words, this is not a duplicate question.

Community
  • 1
  • 1
Berco Beute
  • 1,115
  • 15
  • 30
  • It seems you only read the first part of the answer in the linked question. To return the existing row, you need to use a construct similar to the query in the second part of the answer. – dhke Oct 13 '15 at 14:29
  • You are correct, I totally missed that. That second construct indeed works. Sorry for the confusion. – Berco Beute Oct 13 '15 at 14:36

0 Answers0