3

Possible Duplicate:
Cannot SELECT from UPDATE RETURNING clause in postgres

I am using a PostgreSQL database which has a "company" table and a "country" table which each have a uniqueID.

I want a setup where if I try to add a company to the database, it only adds the company to the database with a new unique ID if it doesn't already exist.

1
  • this has already been asked/solved Commented Dec 16, 2012 at 14:50

1 Answer 1

7
insert into company (unique_id, company_name)
select 42, 'New Company Name'
from company
where not exists (select 1 from company where unique_id = 42);

See also here for a more general solution: Insert, on duplicate update in PostgreSQL?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.