Answer the question
In order to leave comments, you need to log in
How to compose an IF INSERT ELSE UPDATE query?
Good afternoon.
There is a table in which it is necessary to interpose the data with uniqueness check.
Supplier - Name - Brand - Code - Article It is
necessary to insert records with a uniqueness check, but there can be 4 types of uniqueness combinations:
Supplier
+ Article
Supplier + Brand +
Supplier Code + Supplier Code
+ Name
be the following data:
Supplier - Article -
Citylink name + 32542 +
Citylink TV + 32696 + TV
, i.e. the article will be unique, but there will be a conflict in the name, and both lines must be written in the table because they are actually different, for this reason won't fit.ON DUPLICATE KEY UPDATE
I want to insert data through a preliminary
SELECT COUNT(*) FROM table WHERE `Поставщик ` = 1 AND `Артикул ` IS NULL AND `Название` = 'Телевизор' INTO @Var1;
if (@Var1= 0) (INSERT INTO table SET column = 1)
else (UPDATE table SET column = 1 WHERE id = 2)
CASE WHEN @Var1 = 0 THEN (INSERT INTO table SET column = 1)
ELSE (UPDATE table SET column = 1 WHERE id = 2)
END
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question