2012-07-08 2 views
1

Magic the Gathering Cards의 인벤토리 시스템을 만들고 있으며 메인 카드 정보로 가격을 업데이트해야합니다. 에드을 ID, 이름, 가격JOIN, UNION, MERGE?

가격이 다음과 같은 열 : 이름, 에드, 가격

나는 두 테이블, 카드 및 가격

카드는 다음과 같은 열이 있습니다 카드가 필요해. 가격은 가격의 값으로 대체해야합니다. 가격. 다음은

두 개의 다른 시도 (.... 나는 아마 필요 이상이 많이 어려워하고 알고)이 일을하기위한 코드입니다

시도 # 1

$query = "SELECT * FROM Cards"; 

    $result = mysql_query($query) or die(mysql_error()); 
    $row = mysql_fetch_array($result) or die(mysql_error()); 

    while($row = mysql_fetch_array($result)) 
    { 
    $name=$row['Name']; 
    $ed=$row['Ed']; 

    $queryb = "SELECT Price FROM Prices WHERE Name='".$name."' AND Ed='".$ed."'"; 
    $resultb = mysql_query($queryb) or die(mysql_error()); 
    $rowb = mysql_fetch_array($resultb) or die(mysql_error()); 
    $newPrice = $rowb['Price']; 
    mysql_query("UPDATE Cards SET Price='".$newPrice."'"); 
    } 

시도 # 2

$queryCards = "SELECT * FROM Cards"; 
    $queryPrices = "SELECT * FROM Prices"; 
    $dblink = mysql_connect($dbhost, $dbuser, $dbpass); 
    mysql_select_db($dbname, $dblink); 


    $resultCards = mysql_query($queryCards) or die(mysql_error()); 
    $resultPrices = mysql_query($queryPrices) or die(mysql_error()); 
    $rowCards = mysql_fetch_array($resultCards) or die(mysql_error()); 
    $rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error()); 



    if ($rowCards['Name']==$rowPrices['Name'] && $rowCards['Ed']==$rowPrices['Ed']) 
    { 
    $newPrice = $rowPrices['Price']; 
    mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE 
    Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'"); 
    } 
    while($rowPrices = mysql_fetch_array($resultPrices)) 
     { 
     if ($rowCards['Name']==$rowPrices['Name'] && 
        $rowCards['Ed']==$rowPrices['Ed']) 
      { 
      $newPrice = $rowPrices['Price']; 
      mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE 
         Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'"); 
      } 
     }  

    $rowPrices = mysql_fetch_array($resultPrices) or die(mysql_error()); 

    while($rowCards = mysql_fetch_array($resultCards)) 
    { 
    while($rowPrices = mysql_fetch_array($resultPrices)) 
     { 
     if ($rowCards['Name']==$rowPrices['Name'] && 
       $rowCards['Ed']==$rowPrices['Ed']) 
      { 
      $newPrice = $rowPrices['Price']; 
       mysql_query("UPDATE Cards SET Price='".$newPrice."' WHERE 
        Name='".$rowCards['Name']."' AND Ed='".$rowCards['Ed']."'"); 
       } 
       }  
    } 
+0

는 테이블 구조를 제어 할 수 있습니까? 가격 테이블에 외래 키를 실제로 카드 테이블에 놓아야합니다. 그런 다음 sql 문을 실행할 수 있습니다. "업데이트 카드는 가격을 cards.id = price.card_id set cards.price = price.price에 설정합니다." –

+0

테이블 구조가 일관성을 유지하기가 어렵습니다. 카드는 gatherer.wizards.com 데이터베이스에 있습니다. 데이터베이스에서 ID를받은 타사 프로그램의 도움을 받아 데이터를 다운로드 할 수있었습니다. 가격은 http://magic.tcgplayer.com/magic_price_guides.asp에서 제공되며 ID는 제공되지 않습니다. 나는 손을 가격을 복사, 결국 이것을 자동화해야합니다. – user1509556

답변

0
UPDATE Cards 
    JOIN Prices ON Cards.Name = Prices.Name AND Cards.Ed = Prices.Ed 
SET Cards.Price = Prices.Price 
+0

나는 이것을 시도하고 처리하는 데 몇 초가 걸렸지 만, 카드 테이블의 가격은 여전히 ​​비어 있습니다. 언급을 잊어 버렸습니다. Card.Price 필드가 모두 비어있는 것은 중요하지 않습니다. – user1509556

+0

텍스트를 다시 포맷해야만 정상적으로 작동했습니다. Price 테이블 필드의 시작 부분에 휴지통 문자가 있습니다. 당신의 도움을 주셔서 감사합니다. – user1509556