2014-03-27 34 views
0

我需要一個如果,那麼,否則查詢在MySQL中,
嘗試了下面,Mysql查詢更新行如果存在?

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

什麼是做這種正確的方法是什麼?

+0

的可能重複[SQL - IF EXISTS UPDATE ELSE INSERT INTO](http://stackoverflow.com/questions/15383852/sql-if-exists-update-else-insert-into) –

回答

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