简体   繁体   中英

How in MySQL are records added without causing duplicates?

I'm working in MySQL and PHP. How in MySQL do you do an update to an existing table with additional records, without causing duplicates? The records are customers and the key to each record is the customer account number such as "1234".

I need to do basically three things. Add a new customer record if their customer account doesn't exist, update an existing customer's information such as their phone number or e-mail address has changed, or delete the customer record if they are no longer a customer.

The data comes in as a feed daily, and marketing wants to have their own data base of customers with their own fields added, which is why I don't simply re-create the database from the daily feed. The goal is to keep it up to date with the feed. Thanks!

I would suggest that you do all of the processing using PHP.

When you're looping through your customer records, first check if the record exists:

$query = "SELECT customer_account_number FROM table WHERE customer_account_number = 1234";
$result= $mysql -> query($query);
$num   = $result -> num_rows;

if($num == 1){
   // The record already exists so you update.
   $update_query = "UPDATE table SET some_field = 'Some Value' WHERE customer_account_number = 1234";
   $update_result= $mysqli -> query($update_query);
}else{
   // The record doesn't exist so create a record.
   $insert_query = "INSERT INTO table (customer_account_number) VALUES (1234)";
   $insert_result= $mysqli -> query($insert_query);
}

Alternatively, you could do one select query to get the customer account numbers and pop them in an Array.

$query = "SELECT customer_account_number FROM table";
$result= $mysql -> query($query);
$num   = $result -> num_rows;

while($row = $result -> fetch_array(MYSQLI_ASSOC)){

   $cust_records[] = $row['customer_account_number'];

}

// Then for each customer record your looping through.

if(in_array($customer_record_id, $cust_records)){
    // It exists - so just update
    $update_query = "UPDATE table SET some_field = 'Some Value' WHERE customer_account_number = 1234";
    $update_result= $mysqli -> query($update_query);
}else{
    // Doesn't exist - insert
    $insert_query = "INSERT INTO table (customer_account_number) VALUES (1234)";
    $insert_result= $mysqli -> query($insert_query);
}

Also, as suggested by Tadman, if you make sure the customer account number is a UNIQUE column then that will add extra protection to make sure duplicates can't be added.

Run a SELECT id FROM table before inserting and save the ids as keys in an associative array. By simply going if a key is set, you can check if the id exists in the database and do processing based on that.

If you remove each customer process, in the end you'll be left with an array of customers no longer existing.

Executing this one time saves some SQL load.

$result = mysql_query('SELECT id FROM table');
$ids = array();
while($row = mysql_fetch_assoc($result)){
    $ids[$row['id']] = true;
}

foreach($objects as $key => $obj) {
  if($ids[$obj['id']]) {
    // exists, update!
    unset($ids[$obj['id']]);
  } else {
    // new, insert!
  }
}

// go through the remaining people not added
for($ids as $deleted_id) {
  // delete this guy
}

use the insert on duplicate update query. It does the inserting and updating part of your question. For deletion you could have another query i suppose. refer: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

MySQL supports "on duplicate key update" syntax.
This basically allows you to specify which fields to update if the Primary Key (your account number) already exists, all in one SQL statement.

mysql syntax

You'd have to handle deletion with a separate SQL statement, since this logic would almost certainly come from your customer feed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM