Mysql查询以更新行(如果存在)?

Mysql查询以更新行(如果存在)?,mysql,database,database-design,Mysql,Database,Database Design,我需要mysql中的If、then、else查询, 试过下面的, 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... 正确的方法是什么?如果您只需要在mysql中执行此操作,请在重复键上搜索insert。也可以使用存储过程。检查可能的

我需要mysql中的If、then、else查询,
试过下面的,

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...

正确的方法是什么?

如果您只需要在mysql中执行此操作,请在重复键上搜索insert。也可以使用存储过程。检查

可能的副本
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';