It sounds strange to me before the appointment you already have the id
and still check if it exists in the database. But let’s go to the answer!
Whereas you already have the id
of the record to be searched and that id
almost always is a primary key or unique, you can run the following query:
INSERT INTO `pessoas` (`id`, `Name`) VALUES (0, 'João') ON DUPLICATE KEY UPDATE `Name` = 'João';
That’s exactly what you’re doing at query of your question:
If there is some record with the id = 0
, update your Name = 'João'
.
If not, insert the id = 0
with Name = 'João'
.
Just clarify one thing: no
if
you do a search withid = 2
and in theupdate
andinsert
vc assigns theid
0
. It got me a little confused...– LipESprY
Corrected now thank you
– Amadeu Antunes
Okay, I’ll formulate an answer.
– LipESprY
Because all the answers are negative?
– Amadeu Antunes
I don’t understand either.
– Luciano Oiticica Lemgruber
Why is my question now also negative?
– Amadeu Antunes