Questions about how to avoid inserting duplicate data and count the number of duplicate data

Source: Internet
Author: User
There is a set of data that needs to be inserted into the database, such as importing a group of data from EXCEL to the MYSQL database. how can we avoid repeated insertion, here, the repetition is not the repetition of a field, but the combination of multiple fields, for example, idnamecontent & nbsp; 1 Three Meals & nbsp; 2 Li Si's question about avoiding repeated data insertion and counting the number of duplicated data
There is a set of data that needs to be inserted into the database, such as importing a set of data from EXCEL to the MYSQL database, how to avoid repeated insertion, here the repetition does not refer to the repetition of a field, it is a combination of multiple fields, for example:
Id name content
1. three meals
2 Li Si
3. drink water

As shown above, if the values of the name and content fields are repeated, for example, if you insert another "Michael, eat", you will be prompted to repeat and count the number of duplicates and the number of inserted data, evaluate the PHP statement.

------ Solution --------------------
PHP code
$ ExcelData = array ('Zhang San', 'Weiwei'), array ('li Si', 'Weiwei'), array ('Zhang San', 'drinking water '), array ('li Si', 'Eat '), // repeat array ('Zhang San', 'drinking water') // repeat); $ mark = array (); // used as the unique identifier foreach ($ excelData as $ key => $ arr) {$ md5Mark = md5 ($ arr [0]. $ arr [1]); if (isset ($ mark [$ md5Mark]) {unset ($ excelData [$ key]);} else {$ mark [$ md5Mark] = 1 ;}// you can operate $ excelData without judgment, of course, you can also directly combine SQL statements in else or write the database p ($ excelData);/* output: array ([0] => Array ([0] => Zhang San [1] => eat) [1] => Array ([0] => Li Si [1] => eat) [2] => Array ([0] => Zhang San [1] => drinking water ))*/

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.