Onduplicatekeyupdate for simple use-mysql tutorial

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.