3
I have a table with several fields, and I need to make a insert
or replace
on each line. I receive the data coming from a software and if it does not exist, I enter, otherwise I replace.
I get a array
that may have all fields or only a few. Only that there are 2 fields in the table that I am adding.
campo_recebido1
campo_recebido2
campo_recebido3
...
campo_add1
campo_add2
For example, when the campo_recebido1
is equal to 1 I put a value on campo_add1
and when campo_recebido1
is equal to 2 I put a value on campo_add2
.
To do this I use the REPLACE INTO
automatically adjusted with the received fields.
Only I wanted to keep the campo_add1
and the campo_add2
with the last possible values. For this I made a Trigger:
CREATE TRIGGER upd_check BEFORE UPDATE ON table
FOR EACH ROW
BEGIN
IF NEW.`campo_add1` IS NULL THEN
SET NEW.`campo_add1` = OLD.`campo_add1`;
END IF;
IF NEW.`campo_add2` IS NULL THEN
SET NEW.`campo_add2` = OLD.`campo_add2`;
END IF;
END;
I tested with UPDATE and INSERT but seems to do nothing.
How can I always keep campo_add
which are not to be updated but are also not to be deleted?
EXAMPLES
EXAMPLE 1
I get 1,1234,"texto"
and run the query:
REPLACE INTO table SET camporecebido1 = 1, camporecebido2 = 1234, camporecebido3 = "texto", campo_add1 = 32
How can you repair the campo_add2
is not part of the query and will be null
.
EXAMPLE 2
Next I get 2,1234,"textoalterado"
and run the query:
REPLACE INTO table SET camporecebido1 = 2, camporecebido2 = 1234, camporecebido3 = "textoalterado", campo_add2 = 64
How can you repair the campo_add1
is not part of the query, and will be deleted, but I wanted to keep the campo_add1 = 32
of the previous query...
EXAMPLE 3
If in the meantime I get 1,1234
run the query:
REPLACE INTO table SET camporecebido1 =1, camporecebido2 = 1234, campo_add1 = 128
That is, the camporecebido3
will be put to null
, the campo_add1
updated and wanted to keep the meucampo2 = 64
of the previous query.
Set in the Sqlfiddle
Set in DB-Fiddle
Actually, as I understand it, what I need is an equivalent to INSTEAD OF trigger
SQL Server but for Mysql.
But it’s not always update, as I said, it can be Insert if it doesn’t exist. And I don’t want to be checking before if it already exists or not. In addition it can come with empty fields (no field), and I would be leaving other filled fields that should be erased. Because I can only receive one field for update and the rest do not get but have to be deleted.
– Jorge B.
I updated the question with one more example to explain better.
– Jorge B.
In this case you should use the syntax: "Insert into table values (...) on Duplicate update xxxx=yyyyy;"... Check this syntax in https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
– Guilherme Andre
I thought about it, but this form has the same problem in the case of the fields I don’t get, which has to be erased and has no way. Because it has to be transparent, I never know how many fields I will receive from the software, only one identifier, which I considered the
camporecebido2
is that it is mandatory. Others may not even come, and if none other than the ID comes in, the others must be deleted if the record already exists.– Jorge B.
If they have to be deleted, replace already does this
– Guilherme Andre
But there’s the replace has the problem I said above. that’s the
meucampo1
(or 2) that must be maintained if it is already set and not to be modified.– Jorge B.