MySQL insert values only if non exists in unique key combination

38 Views Asked by At

in one of my tables, there is a unique key based on two columns. One must be a value (NOT NULL) and the other may be NULL.

For the logic behind the issue, I made this table schema;

CREATE TABLE `testinserttable` (
  `RowId` int unsigned NOT NULL AUTO_INCREMENT,
  `SomeId` varchar(20) NOT NULL,
  `UsedForUniqueKey` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`RowId`),
  UNIQUE KEY `UniqueKey` (`SomeId`,`UsedForUniqueKey`)
) ENGINE=InnoDB 

and added these values

enter image description here

now I would like to ONLY add SomeId, when it doesn't exists in the table in whatever combination with UsedForUniqueKey. And, also important, I would like to do this in a bulk.

So, for instance, I would like to bulk add 'Bye','ola','Hi' to the table, if the value does not exist. Bye exists, so this should be skipped. I have seen working examples with one row, but not with bulk. Any idea on how to get this working in a single statement?

regards,

Matthijs

0

There are 0 best solutions below