getConnection(); //get all duplicated emails $select = $conn->select() ->from($this->getTable('customer/entity'), array('email', 'website_id', 'cnt' => 'COUNT(*)')) ->group('email') ->group('website_id') ->having('cnt > 1'); $emails = $conn->fetchAll($select); foreach ($emails as $data) { $email = $data['email']; $websiteId = $data['website_id']; $select = $conn->select() ->from($this->getTable('customer/entity'), array('entity_id')) ->where('email = ?', $email) ->where('website_id = ?', $websiteId); $activeId = $conn->fetchOne($select); //receive all other duplicated customer ids $select = $conn->select() ->from($this->getTable('customer/entity'), array('entity_id', 'email')) ->where('email = ?', $email) ->where('website_id = ?', $websiteId) ->where('entity_id <> ?', $activeId); $result = $conn->fetchAll($select); //change email to unique value foreach ($result as $row) { $changedEmail = $conn->getConcatSql(array('"(duplicate"', $row['entity_id'], '")"', '"' . $row['email'] . '"')); $conn->update( $this->getTable('customer/entity'), array('email' => $changedEmail), array('entity_id =?' => $row['entity_id']) ); } } /** * Add unique index for customer_entity table */ $conn->addIndex( $this->getTable('customer/entity'), $this->getIdxName( 'customer/entity', array('email', 'website_id'), Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE ), array('email', 'website_id'), Varien_Db_Adapter_Interface::INDEX_TYPE_UNIQUE );