Correct batch update counters in MySQL Databases

Source: Internet
Author: User

The following articles mainly describe the actual operation steps for performing batch update counters in the MySQL database. Assume that there is a counter in our MySQL database, and we need to re-count this counter, so how should we set it?

For example, in a mall, every time a product is sold, a flow occurs. Then, we need to know that each flow is the first time the product was sold. This may not be clear, let me take a detailed data example.

 
 
  1. recordID,productID,productType,sellDate,counter  
  2. 1, 1, 1, '2010-1-15 15:20:10' 0  
  3. 2, 1, 2, '2010-1-15 15:20:10' 1  
  4. 3, 2, 1, '2010-1-15 15:20:10' 0  
  5. 4, 2, 1, '2010-1-15 15:20:10' 1 

The above data is a bit of data, including the recorded serial number, product number, product type, sales time, and counter. In general, we will first think of auto-increment counters, but it is certainly impossible to use auto-increment counters.

At first, I tried to use the following code:

 
 
  1. update t_product set t_counter = (select max(counter)   
  2. from t_product where productid = 1 and productType = 1) + 1   
  3. where where productid = 1 and productType = 1 

However, MySQL reports an error and checks the Internet. MySQL database does not support this method. I am not familiar with MySQL. I remember that I used a cursor on SQL server before, and then I tried to find the cursor. After n times of google and Baidu, I finally solved this problem.

I am a layman in MySQL, and I don't even know how to comment the annotations. Now I finally know there are three types of writing methods, and -- the annotations are left blank, so I can't say anything about the performance of code writing. I just implemented this function and hoped to be useful to friends who have the same requirements.

Well, I also learned that declare can only be written at the beginning.

Check the final code:

 
 
  1. CREATE PROCEDURE p_resetCounter ()  
  2. BEGIN  
  3. DECLARE productID INT;  
  4. DECLARE type INT;  
  5. DECLARE tmpCount INT;  
  6. DECLARE stopFlag int; 

Use cursor
 

 
 
  1. DECLARE cur cursor for SELECT COUNT(*), productID, productType FROM  
  2. t_product GROUP BY productID, productType;  
  3. DECLARE CONTINUE HANDLER FOR NOT FOUND set stopFlag=1; 

If no record is found, set stopFlag to 1.

Define variables and create temporary tables
 

 
 
  1. CREATE TEMPORARY TABLE tmp_Counter(  
  2. recordID int not null,  
  3. Counter int not null  
  4. )TYPE = HEAP; 

Open cursor
 

 
 
  1. open cur;  
  2. REPEAT  
  3. fetch cur into tmpCount, productID, type;  
  4. SET @id = -1;  
  5. INSERT INTO tmp_Counter   
  6. (SELECT recordID, (@id := @id + 1) counter   
  7. from t_product WHERE productIDproductID = productID AND productType = type)  
  8. ORDER BY ts_Date ASC;  
  9. UNTIL stopFlag = 1 
  10. END REPEAT;  
  11. close cur; 

Close cursor
 

 
 
  1. UPDATE t_product, tmp_Counter SET counter = tmp_Counter.Counter   
  2. WHERE recordID = tmp_Counter.recordID;  
  3. -- SELECT * FROM tmp_Counter;  
  4. DROP TABLE tmp_Counter; 

Delete temporary table
 

 
 
  1. END; 

The above content is an introduction to how to update counters in batches in the MySQL database. I hope you will have some gains.

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.