I am attempting to write a query that will update the record if it exists, or create it if it does not, however I want to do that based on the condition that text_value
is not already set.
I initally thought of using REPLACE
, but that does not support a WHERE
clause.
REPLACE INTO settings
SET var_name = '',
var_group = '',
text_value = '',
country_id = ''
Can I do what I want to do in one query? I am trying to do so without having to write something like this where I have two queries in my PHP:
SELECT FROM settings WHERE text_value != '' AND country_id = $country_id
IF that returns no results then
INSERT INTO settings...
This is my table DDL information:
CREATE TABLE `settings` (
`settings_id` int(11) NOT NULL auto_increment,
`var_name` varchar(50) default NULL,
`var_group` varchar(26) NOT NULL,
`text_value` text NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY (`settings_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8