Shirish11
Shirish11

Reputation: 1617

PostgreSQL match multiple values between tables

I have this query to insert data which is not present inside mytab from temptab

 INSERT INTO mytab SELECT * FROM temptab 
  WHERE NOT EXIST (SELECT  * FROM mytab WHERE 
    (mytab .col1= temptab .col1 AND mytab .col2=temptab .col2))  

I want to know if there is any other way of writing this query's condition part i.e. the last statement.
The above query works absolutely fine but becomes too lengthy when matching many more columns. So I was thinking if there is any kind of format some what like

 mytab.(col1,col2,...,coln)=temptab.(col1,col2,...,coln)  

I need to match so many columns since combination of these works as primary key for my table.Similary there exist many more tables.

any views appreciated.

P.S. : don't hesitate to improve the query.

Upvotes: 0

Views: 1410

Answers (2)

Kouber Saparev
Kouber Saparev

Reputation: 8105

INSERT INTO
  mytab
SELECT
  *
FROM
  temptab 
WHERE
  (temptab) NOT IN (
    SELECT (mytab) FROM mytab
  );

Upvotes: 3

Szymon Lipiński
Szymon Lipiński

Reputation: 28634

you can write it like this:

(mytab.col1, mytab.col2) = (temptab.col1, temptab.col2)

Upvotes: 2

Related Questions