Suppose you have 5 columns in your table - col1, col2, col3, col4, col5. And suppose the data corresponding to these columns that you are trying to insert is in variables - $ col1, $ col2, $ col3, $ col4, $ col5 (I assume that PHP is your language, but please change the format of the variables according to your nomenclature).
So your insert might look like this:
INSERT INTO `tableA` (`col1`, `col2`, `col3`, `col4`, `col5`) SELECT $col1, $col2, $col3, $col4, $col5 FROM `tableA` WHERE NOT EXISTS (SELECT 1 FROM `tableA` WHERE `col1` = $col1 AND `col2` = $col2 AND `col3` = $col3 AND `col4` = $col4 AND `col5` = $col5);
Another alternative could be:
INSERT INTO `tableA` (`col1`, `col2`, `col3`, `col4`, `col5`) SELECT $col1, $col2, $col3, $col4, $col5 FROM `tableA` WHERE `col1` = $col1 AND `col2` = $col2 AND `col3` = $col3 AND `col4` = $col4 AND `col5` = $col5 HAVING COUNT(1) = 0;
Hope this helps.
Abhay source share