B
B
baimkin2020-02-25 14:28:45
MySQL
baimkin, 2020-02-25 14:28:45

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;


Tried:
if (@Var1= 0)  (INSERT INTO table SET column = 1)
else (UPDATE  table SET column = 1 WHERE id = 2)


and
CASE WHEN @Var1 = 0 THEN (INSERT INTO table SET column = 1)
ELSE  (UPDATE  table SET column = 1 WHERE id = 2)
END


Prompt how to do an insert of record with preliminary check of a condition.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-02-25
@Rsa97

You already decide. Either "Supplier + Name" is a unique pair, or duplicates of this pair are possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question