Random query records and batch insertion of SQL statements-Based on mysql and SQL statement-mysql
This week, I encountered several database problems. One is the requirement for copying fields between tables due to modifying the data structure mentioned in the previous article, and the other is what I want to write in this article: 1. Randomly select several hundred records from a data table based on a field that is not repeated; 2. insert these records and other values into the data table to form a complete row record.
1. Cause
Buddha said that there is a cause and effect in everything. I don't blame anyone (in fact, I don't trust Buddha, and I can't help it ). This is the case: we have developed a simple voting system, and the system has been running smoothly until last weekend ...... in that day, I was sitting in the Zen seat and closed my eyes and realized that I was about to settle. Suddenly, a white light was shining, and a few words were carried in the white light: "S master's work was suddenly lost on the webpage, check the problem as soon as possible. Another note: this work is very popular, and eight hundred viewers have voted. "The white light shows the faint emblem of the product's adults. That is the portrait of an adult.
I immediately replied, "Please follow the product's will and do it right away ". As a result, ssh to the server, mysql to the database client, according to the name of the S master, found that: S master only user registration records, but the work records are empty. I will immediately reply to the product adult: "S master has registered in our system, but he has not input any work ".
The product adult replied: "Is the system under hacker attack? Please check whether system vulnerabilities are exploited by attackers to steal and destroy data remotely and find Database backups, be sure to restore the work information of S master ".
I thought for a moment and replied, "follow suit".
So I immediately opened the code, checked the logic, and realized that the system developed by our company is based on identity authentication. the upload, edit, delete, and voting of works must be authenticated, otherwise, no logon is sent, and no malicious requests are initiated. Then, I checked the deletion logic of the file. The Delete action only allows POST requests, and the session mechanism is used here. Only the file owner can delete the file. Then, I checked several Database backups and found that no trace of S master's work was found at these time points.
As a result, I replied to the product adult's inspection results.
After a while, the product owner said, "I have discussed with S, asked him to re-upload the work, and then you can restore the number of votes to him ".
I replied, "OK ".
2. For details, refer to recruitment and removal.
As a monk who has just entered the Internet, he knows that the vast majority of rivers and lakes are boundless. He does not say that his cultivation methods are tens of millions. Even the classics and skills of a certain school are complicated. For example, if you want to complete the tasks of the above product, you will be involved in the mysql technology. Make up the vote record of the S-master. If there are ten or eight votes, it's a big deal, on the mysql client, just repeat the following command for ten or eight times.
Insert into values (File id, voter id, and voting time );
The problem is that there are 800 records, which are also very labor-intensive. In addition, for the S master's work, the author id in the voting record should be registered in the system and cannot be repeated, and the voting time cannot all be one time. It seems that although Shaolin Changquan is brave and meets this opponent, it is under the storm. You can only do this: Randomly extract 800 non-repeated user records from the existing voting record table, and then create a complete record row with the corresponding fields, and then insert the records into the data table.
3. Trick
Zhao Yi, Shaolin Xiaohua finger-SQL random capture.
SELECT distinct voter idFROM voting table WHERE voter id> = (select max (voter id) FROM voting table)-(select min (voter id) FROM voting table) * RAND () + (select min (voter id) FROM voting table)
LIMIT 800;
Trick 2: automate Shaolin Changquan-php to construct SQL insert statements
1 // database connection process and $ sql1 omitting 2 3 $ stmt1 = $ db-> prepare ($ sql1); 4 $ e = $ stmt1-> setFetchMode (PDO :: FETCH_NUM); 5 $ stmt1-> execute (); 6 $ r1 = $ stmt1-> fetchAll (); 7 8 $ time = strtotime ('2017-02-20 08:00:00 ') + 60; 9 10 // concatenate and insert SQL statements 11 $ sql2 = 'insert into voting table values'; 12 foreach ($ r1 as $ row) {13 $ sql2. = '('. S master id. ','. $ row [0]. ','. "'". date ('Y-m-d H: I: s', $ time ). "'". '),'; 14 $ time + = 60; 15} 16 $ sql2 = rtrim ($ sql2 ,','). '; 17 18 $ stmt2 = $ db-> prepare ($ sql2); 19 $ r2 = $ stmt2-> execute ();
If you have these two tactics, you will be able to overcome these challenges.
4. Postscript
This is not over yet. S master's work is lost in the end, there is still no clue, which of the following heroes and experts in the rivers and lakes have seen the flaws in the hope to give advice one or two.
End
All rights reserved. If you can see the following tricks, you can just take them away and indicate the rights reserved and source links.
References:
Mysql randomly obtains one or more data records.