1. Recently, when I was working on a project, I had to update the statistical table every time I had done one thing. However, I had to update the statistical table, check whether such a record already exists in the statistical table based on the primary key. if so, update the record. if not, insert a record. this is what I did at first, the result is rejected by the boss.
1. Recently, when I was working on a project, I had to update the statistical table every time I had done one thing. However, I had to update the statistical table, check whether such a record already exists in the statistical table based on the primary key. if so, update the record. if not, insert a record. this is what I did at first, the result is rejected by the boss.
1. Recently, when I was working on a project, I had to update the statistical table every time I had done one thing. However, I had to update the statistical table, check whether such a record already exists in the statistical table based on the primary key. if so, update the record. if not, insert a record. this is what I did at first, the result was rejected by the boss. He said he could use on duplicate key update. Perform the following operations:
Table structure:
+ ----------- + --------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ----------- + --------- + ------ + ----- + --------- + ------- +
| Player_id | int (11) | NO | PRI | NULL |
| Count | int (11) | YES | NULL |
+ ----------- + --------- + ------ + ----- + --------- + ------- +
2 rows in set (0.01 sec) <无>
Select * from player_count where player_id = 1; // query whether there is an insert into player_count (player_id, count) value () in the statistical table ); // If No record exists, execute the insert operation update player_count set count = count + 1 where player_id = 1; // if there is a record, execute the update operation.
insert into player_count(player_id,count) value(1,1) on duplicate key update count=count+1;