3
How can I in Mysql give a INSERT
or a UPDATE
if the line in question already exists or not?
I tried it this way but it didn’t work:
IF EXISTS (SELECT * FROM configs WHERE id_serie = :id)
UPDATE configs
SET dados = :valor
WHERE id_serie = :id
ELSE
INSERT INTO configs (id_serie, dados) VALUES (:id, :valor)
But it didn’t work, I read about ON DUPLICATE KEY UPDATE
but as the column I use to verify in the case id_serie
is not the primary key of the table, I imagined it would not work, or works?
But in this case, if I change only one column, the new record will get all the values from the other old columns and insert into the new one updating only what I need?
– Leo Letto
No. If you leave a column without informing, you will lose the value of these fields.
– Filipe Martins
It doesn’t seem very safe to do this way, I think I’ll end up having to combine a little PHP with this UPDATE / INSERT
– Leo Letto