4
I have a query that takes the data from a . csv and inserts the data into the table.
If the position is the same it updates the fields. I need that in addition to checking the position the query check whether status is different from 1 (status <> 1) to update.
That one status is not passed by . csv is only registered in the database and cannot update the records that are with status = 1.
How to do this?
$import=
"INSERT INTO registros (NAME, position, price)
VALUES
(
'$data[1]',
'$data[2]',
'$data[3]'
) ON DUPLICATE KEY UPDATE position = position,
NAME = '$data[1]',
position = '$data[2]',
price = '$data[3]',
qty_try = 0";
The value of
statusis that of the record where theDUPLICATE KEYwas found.– Thomas
That query worked, thank you very much Thomas. the status value is a field that is changed by another query called through an api when I pass a url with name && price, then in this query it changes the status to 1. In the case of my question is a query with database updates made through an import. I couldn’t try the project because all I have to do is go to the work bank, but thank you for helping Bacco
– Rafael Ribeiro
@Rafaelribeiro If you tested and solved, I think it is a good way out. You cannot specify a condition in the UPDATE itself, but as an argument to IF inline to keep the parameter equal or not, according to the
status, is an interesting output (and has my +1).– Bacco
@Bacco just wouldn’t need to if
positionwere theDUPLICATE KEYin question. It would be good to confirm with the owner of the question before changing.– Thomas
@Bacco Good, in this case then you can say that ON DUPLICATE KEY will run for the values of
positionthat were violated, since in INSERT it does not specify the PRIMARY KEY. So yes, you can even edit the answer. Well remembered– Thomas
@Bacco Ah, until they are didactic :)
– Thomas