wolfgang
wolfgang

Reputation: 7799

Mysql query to update row if exists?

I need an If, then, else query in mysql,
tried out the below,

if exists( select * from data_table where user_id =1 and link_id = 1) then update data_table set is_view = 1 where user_id = 1 else insert into data_table...

what is the correct way to do this?

Upvotes: 0

Views: 75

Answers (2)

Roshan
Roshan

Reputation: 1459

insert into data_table (user_id, link_id, other_column)
values (1, 1, 'value to insert or uodate')
on duplicate key update  other_column='value to insert or update';

Upvotes: 1

kruger
kruger

Reputation: 26

if you only need to do this in mysql, then search insert on duplicate key. Or you can use a stored procedure. Check INSERT ... ON DUPLICATE KEY UPDATE Syntax

Upvotes: 1

Related Questions