sql - mysql if exists -


i've been staring @ while now. maybe fresh eyes able point out im doing wrong.. here query:

if (exists (select * `admin_rule_module` `rule_set_id`='13' , `entity_id`='2'))  begin  update `admin_rule_module`  set `permission`='allow' `entity_id`='2'  end else  begin  insert `admin_rule_module` (`rule_set_id`, `entity_id`, `permission`)  values ('13', '2', 'allow')  end 

i'm getting following error:

you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'if (exists (select * admin_rule_module rule_set_id='13' , `enti' @ line 1

try this...

insert admin_rule_module values (13, 2, 'allow')  on duplicate key update permission = 'allow', entity_id = 2; 

assuming you've unique key key (rule_set_id, entity_id), @ least. if want conditional , multiple statements, have put in procedure or function.


Comments