简体   繁体   English

如果存在,mysql查询更新行?

[英]Mysql query to update row if exists?

I need an If, then, else query in mysql, 我在mysql中需要一个If,then,else查询,
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? 这样做的正确方法是什么?

if you only need to do this in mysql, then search insert on duplicate key. 如果您只需要在mysql中执行此操作,则在重复键上搜索插入。 Or you can use a stored procedure. 或者您可以使用存储过程。 Check INSERT ... ON DUPLICATE KEY UPDATE Syntax 检查INSERT ... ON DUPLICATE KEY UPDATE语法

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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM