Check if row exists, if not add it [MySQL]
- by Steven
I need to add a row to a MySQL database table but only if the row doesn't already exist. My database server just went down so I can't test this, but will this work as expected?
INSERT INTO `blocks` (`block_file`,`settings_group`)
VALUES ('announcements','announcement_settings')
WHERE NOT EXISTS (SELECT `block_file`,`settings_group`
FROM `blocks`
WHERE `block_file`='announcements' AND `settings_group`='announcement_settings')
It seems like sound logic. Is this a valid query or is there a better way of doing this?