How to Update a record if it exists otherwise insert a new record in MySQL -
i trying write mysql query update record if exists otherwise create new record 2 different tables. found information online can't seems nail it.
this current query
set @time_now := now(); if exists(select 1 phone_calls account_id = 8 , call_code_id = 5 , status = 1) begin update phone_calls set trigger_on=@time_now, isappointment=1, modified_by = 2, modified_on = @time_now account_id =8 , call_code_id = 5 , status = 1; end else begin insert phone_calls (mid_id, account_id, call_code_id, trigger_on, created_on, call_subject, status, next_call_id , call_direction, owner_id, workflow_generated, call_notes, total_attempts, isappointment) values (1, 8, 5, @time_now, @time_now, 'this test', 1, 0 , "outbound", 2, 1, "", 0, 0); insert inventory_engine_history(phone_call_id, new_phone_call_id, created_by, new_call_owner, action, trigger_on) values(1000, last_insert_id(), 2, 2, '', @time_now)'; end
i running syntax error
#1064 - have error in sql syntax; check manual corresponds mysql server version right syntax use near 'if exists(select 1 phone_calls account_id = 8 , call_code_id = 5 an' @ line 1
can please me error?
note: if multiple records exists should update of them if no record exist them insert 1 record in each table.
thanks
declare thecount int; select count(*) thecount phone_calls account_id = 8 , call_code_id = 5 , status = 1 if(thecount=0) -- dologic; else -- dosomeotherlogic; end if
Comments
Post a Comment