0

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?

wolfgang
  • 7,281
  • 12
  • 44
  • 72
  • possible duplicate of [SQL - IF EXISTS UPDATE ELSE INSERT INTO](http://stackoverflow.com/questions/15383852/sql-if-exists-update-else-insert-into) – Sunny Sharma Mar 27 '14 at 06:00

2 Answers2

1

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

kruger
  • 26
  • 4
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';
VMAtm
  • 27,943
  • 17
  • 79
  • 125
Roshan
  • 1,459
  • 1
  • 14
  • 20