I believe you need to undo your logic for it to work:
insert into the table - if it exists (the same key), then update it.
this can be achieved using the ON DUPLICATE as follows:
INSERT INTO Table1 VALUES(...) ON DUPLICATE KEY UPDATE column=column+1
check the manual here
source share