3
In Postgresql you can use the upsert
. It checks if the line already exists, if yes it does update, otherwise it does Insert.
In Mysql I’m not able to do this, I saw some talking to use INSERT ... ON DUPLICATE KEY UPDATE
, but all the examples I saw were informing id.
However I do not have the ID, the ID of my table is generated automatically to every Insert I do, so I do not inform this ID at the time of making an Insert.
I found a solution in Github, maybe I can help you @Isaias.
– Marconi
your table beyond the id column has no other one with unique and unique values?
– user60252
@Leocaracciolo has yes, for example, in a table with user data, a unique value could be the email he uses to log in. If the email exists, update, otherwise update.
– Isaias Tavares
So what exactly is your difficulty? ride that if Else?
– user60252
your
PK
isauto_increment
?– Ricardo Pontual
@Leocaracciolo Yes, mount if Else using pure sql only. In Postgresql, for example, I just use the UPSERT function, but in Mysql I don’t know how to do it.
– Isaias Tavares
@Ricardopontual Yes
– Isaias Tavares
In that case you won’t be able to use
insert on duplicate
(https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html) and thereplace
(https://dev.mysql.com/doc/refman/5.7/en/replace.html). You will probably need to make oneIF
to solve this– Ricardo Pontual
Vlw, by the way it will be easier to do in the same code. kkk
– Isaias Tavares