I have a database table containing a 'key' field which holds a system generated key.
| id | key | etc. |====|=====================|================= | 1 | osi32i4kjwlqe | |----|---------------------|----------------- | 2 | klwjeklj1343k | |----|---------------------|----------------- | 3 | dfk32ld32ll23 | |----|---------------------|---------------- | 4 | lk3jkl3j42lk4 | --------------------------------------------
In addition, I have a list of keys:
{'osi32i4kjwlqe','ksadalala12','klwjeklj1343k','dfk32ld32ll23','lk3jkl3j42lk4','askalksl56'}
Note, that some of the keys on the list exist in the database, and others do not.
I would like, if I can, to run an efficient query that will go through the list, and affirm the existence of the key in the database.
ie: if the key exists, do nothing. if it doesn't, insert a fresh row with this key.
Does anybody know of an efficient way to do it?
Thank you.
I assume that your key column is declared with UNIQUE
. If so, just loop and use
INSERT IGNORE INTO table ...
for each of your keys from JSON array. If not, well, make so as I think this is what you want to accomplish anyway :)