2014-03-31 7 views
0

제 질문은 제목으로 설명하기 어려워서 데이터와 목표를 보여 드리겠습니다. 다음과 같은 구조의 MySQL의 테이블이 있습니다 :유사한 행의 값으로 행 null 필드 업데이트 (동일한 '키')

CREATE TABLE customerProjectData(
    idCustomer INT NOT NULL, 
    idProject INT DEFAULT NULL, 
    comePersons SMALLINT DEFAULT NULL, 
    comePairs SMALLINT DEFAULT NULL, 
    comment VARCHAR(255) DEFAULT NULL, 
    idCity INT DEFAULT NULL, 
    idStreet INT DEFAULT NULL, 
    name VARCHAR(64) DEFAULT NULL, 
    surname VARCHAR(64) DEFAULT NULL, 
    homeNum VARCHAR(10) DEFAULT NULL, 
    postCode CHAR(6) DEFAULT NULL, 
    postCity VARCHAR(64) DEFAULT NULL, 
    cellPhone VARCHAR(12) DEFAULT NULL 
) 

것은이가,이 또한 PRIMARY KEY(idCustomer, idProject) 정의하고 그렇지 않아 놈이야. 결과 중복되는 (동일한 기본 키로) 데이터가 있지만 종류가 있습니다.

나는 ALTER IGNORE TABLE을 실행할 수 있지만 데이터 손실은 아마도 용납 될 수없고 예측할 수 없을 것입니다. 마지막으로 데이터가 포함되어 있고 그 후에 ALTER IGNORE TABLE을 실행하면 중복 된 값으로 null 필드를 채우기로했습니다. 그렇게 적은 데이터가 손실 될 수 있습니다.이 경우에는 허용됩니다 (이제는 더 오랜 시간이 지나면 그대로 두는 것이 낫습니다).

질문은 각 복제본에서 해당 입력란을 채우는 방법입니다.

답변

1

여기 대략적인 시도입니다.

먼저 번호를 찾으십시오. 동일한 키가있는 행

<?php 
// $link is the database identifier 

$sql = 'SELECT COUNT(*) AS num, * FROM `customerProjectData` GROUP BY `idCustomer`, `idProject` HAVING COUNT(*) > 1 ORDER BY COUNT(*) ASC;'; 
$run = mysql_query($sql, $link); 

$rows = array(); 
if($run && mysql_num_rows($run)) { 
    while(($fetch = mysql_fetch_assoc($run)) !== false) { 
     $rows[] = $fetch; 
    } 
} 
?> 

지금 $rows는 같은 키와이 키가 테이블에 반복 된 횟수의 count이있는 모든 행의 목록이 포함되어 있습니다.

count 번을 반복하고 어떤 행에 전체 데이터가 있는지 확인한 다음이 레코드의 데이터로 다른 레코드를 채우는 데 사용할 수 있습니다.

시행 착오의 비트.

+0

좋아요. 내가 정확히 SQL 프로 시저 에서이 일을하려고 해요 :] – Joe

0

나는 @ 웹 - 유목민 제안을 사용하고 비슷한했지만 SQL 절차 : 도움을

DROP PROCEDURE IF EXISTS correctCPD$$ 
CREATE PROCEDURE correctCPD() 
BEGIN 

    DECLARE currentCustomerId INT; 
    DECLARE currentProjectId INT; 
    DECLARE cur_idCustomer INT; 
    DECLARE cur_idProject INT; 
    DECLARE cur_comePersons SMALLINT; 
    DECLARE cur_comePairs SMALLINT; 
    DECLARE cur_comment VARCHAR(255); 
    DECLARE cur_idCity INT; 
    DECLARE cur_idStreet INT; 
    DECLARE cur_name VARCHAR(64); 
    DECLARE cur_surname VARCHAR(64); 
    DECLARE cur_homeNum VARCHAR(10); 
    DECLARE cur_postCode CHAR(6); 
    DECLARE cur_postCity VARCHAR(64); 
    DECLARE cur_cellPhone VARCHAR(12); 

    CREATE TEMPORARY TABLE ids (
     idCustomer INT, 
     idProject INT 
    ) ENGINE = InnoDB; 

    INSERT INTO ids 
     SELECT idCustomer, idProject FROM customerprojectdata group by idCustomer, idProject having count(*) > 1; 

    BLOCK1: BEGIN 
     DECLARE done INT DEFAULT FALSE; 
     DECLARE itemCur CURSOR FOR SELECT idCustomer, idProject FROM ids; 
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 

     OPEN itemCur; 

     itemCurLoop: LOOP 
      FETCH itemCur INTO currentCustomerId, currentProjectId; 
      IF done THEN 
       LEAVE itemCurLoop; 
      END IF; 

      BLOCK2: BEGIN 
       DECLARE doneIn INT DEFAULT FALSE; 
       DECLARE cpdCur CURSOR FOR SELECT idCustomer, idProject, comePersons, comePairs, comment, idCity, idStreet, name, surname, homeNum, postCode, postCity, cellPhone FROM customerProjectData WHERE idCustomer = currentCustomerId AND idProject = currentProjectId;  
       DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneIn = TRUE; 

       OPEN cpdCur; 
       cpdCurLoop: LOOP 
        FETCH cpdCur INTO 
         cur_idCustomer, cur_idProject, cur_comePersons, cur_comePairs, 
         cur_comment, cur_idCity, cur_idStreet, cur_name, cur_surname, 
         cur_homeNum, cur_postCode, cur_postCity, cur_cellPhone; 
        IF doneIn THEN 
         LEAVE cpdCurLoop; 
        END IF; 

        UPDATE CustomerProjectData SET 
         comePersons = IF((comePersons IS NULL OR comePersons = '') AND cur_comePersons > 0, cur_comePersons, comePersons), 
         comePairs = IF((comePairs IS NULL OR comePairs = '') AND cur_comePairs > 0, cur_comePairs, comePairs), 
         comment = IF((comment IS NULL OR comment = '') AND cur_comment > 0, cur_comment, comment), 
         idCity = IF((idCity IS NULL AND idStreet IS NULL) AND cur_idCity > 0, cur_idCity, idCity), 
         idStreet = IF(((idCity IS NULL OR idCity = cur_idCity) AND idStreet IS NULL) AND cur_idStreet > 0, cur_idStreet, idStreet), 
         name = IF((name IS NULL OR name = '') AND cur_name > 0, cur_name, name), 
         surname = IF((surname IS NULL OR surname = '') AND cur_surname > 0, cur_surname, surname), 
         homeNum = IF((homeNum IS NULL OR homeNum = '') AND cur_homeNum > 0, cur_homeNum, homeNum), 
         postCode = IF((postCode IS NULL OR postCode = '') AND cur_postCode > 0, cur_postCode, postCode), 
         postCity = IF((postCity IS NULL OR postCity = '') AND cur_postCity > 0, cur_postCity, postCity), 
         cellPhone = IF((cellPhone IS NULL OR cellPhone = '') AND cur_cellPhone > 0, cur_cellPhone, cellPhone) 
        WHERE idCustomer = currentCustomerId AND idProject = currentProjectId; 

       END LOOP; 
       CLOSE cpdCur; 
      END BLOCK2; 
     END LOOP; 

     CLOSE itemCur; 

    END BLOCK1; 

    DROP TEMPORARY TABLE ids; 

END$$ 

감사합니다!