sql - Update query on MySQL only when two conditions exist, otherwise Insert ( -
sql - Update query on MySQL only when two conditions exist, otherwise Insert ( -
i have table this:
id id_user source content
or just:
create table `p_l_0215` ( `id` bigint(20) not null auto_increment, `id_user` bigint(20) not null, `source` varchar(50) not null, `content` text not null, key `id` (`id`) ) engine=myisam default charset=utf8 auto_increment=2 ;
i want update when id_user=value1 , source='default' if status not exist want insert. know method when have duplicate key, in table id_user neither source unique. how can solve this? help!
1) create procedure
delimiter // create procedure smartinsert(given_id_user bigint(20),given_source varchar(50), given_content text) begin if (exists(select * p_l_0215 `id_user`=given_id_user , `source`=given_source)) update p_l_0215 set `content`=given_content `id_user`=given_id_user , `source`=given_source; else insert p_l_0215 values (null, given_id_user, given_source, given_content); end if; end// delimiter ;
2) phone call call smartinsert(13, 'some_source', 'some content');
mysql sql
Comments
Post a Comment