If mysql exists, it is updated. if it does not exist, it is inserted. how can I write it? if mysql exists, it is updated. if it does not exist, it is inserted, how can I write a table like friend_idcomment_num, which records comments on Friends? when this data exists, it is directly Updated. if it does not exist, insert it first. what can I do? thank you, PHPcodeREPLACE mysql is updated if it exists. if it does not exist, it is inserted. how can I write it?
Mysql is updated if it exists. if it does not exist, it is inserted. how can I write it?
Such as a table that records comments on friends
Friend_id comment_num
If this data entry exists, it is directly Updated. if it does not exist, insert it first. how can this problem be solved? thank you.
------ Solution --------------------
PHP code
Replace into 'Table' (friend_id, comment_num) VALUES (...);
------ Solution --------------------
If an old record in the table has the same value as a new record used for the primary key or a UNIQUE index, the old record is deleted before the new record is inserted.
Unless the table has a primary key or UNIQUE index, it makes no sense to use a REPLACE statement.
Discussion
PHP code
Replace into 'Table' (friend_id, comment_num) VALUES (...);
Record modified and inserted