I would like to insert into table if table does not already contain those values.
This is my table
ID | Col1 | Col2 | Col3 | Col4
My table contains the following data:
0 | "a1" | "a2" | "a3" | "a4" |
1 | "b1" | "b2" | "b3" | "b4" |
2 | "c1" | "c2" | "c3" | "c4" |
I would like to not allow insert of the following input:
3 | "b1" | "b2" | "b3" | "d4" |
But allow the following:
3 | "a1" | "b2" | "c3" | "d4" |
I have tried the following
INSERT INTO my_table (Col1, Col2, Col3, Col4) SELECT * FROM (SELECT :val1, :val2, :val3, :val4) as tmp WHERE NOT EXISTS (SELECT 1 FROM my_table WHERE Col1=:val1 AND Col2=:val2 AND Col3=:Val3)
This works fine unless :val1==:val2.
Is there a better way to insert and avoid duplicates?