0

I want to insert multiple records or update those records if 2 specific column values do not exist in a record. But the 2 values are NOT UNIQUE, so I can not use ON DUPLICATE KEY. I am trying to not use IF...Then statements. It seems to be bad programming practice.

Here is the pseudo code of what I want to accomplish.

INSERT INTO interest_list (user_id, interest_id, details)
  VALUES
  (1, 1,'somestring1'),
  (1, 2, 'somestring2'), 
 ...etc
IF the record exists where (user_id==1 and interest_id==1) or (user_id==1 and interest_id==2)  ... etc THEN (dont insert but rather update)
UPDATE interest_list
    SET details = CASE interest_id
        WHEN 1 THEN 'somestring1'
        WHEN 2 THEN 'somestring2'
...etc
    END
WHERE interest_id IN (1,2...etc) AND user_id=1;

Its fairly complicated to explain, hopefully someone gets my question. I am trying to accomplish this in 1 query.

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by baki100
0

For instances like this i always suggest using anonymous blocks or stored procedures and you are probably most likely going to need an if statement

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.