php - 加入、联合、合并?

标签 php mysql

我正在为 Magic the Gathering Cards 创建库存系统,需要使用主要卡片信息更新价格。

我有两个表,卡片和价格

卡片有以下列: ID、姓名、Ed、价格

价格有以下列: 姓名、Ed、价格

我需要将 Cards.Price 替换为 Prices.Price 中的值。

下面是两种不同尝试的代码(我知道我可能使它变得比需要的更难......)

尝试#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']."'");
                }  
                }      
    }

最佳答案

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

关于php - 加入、联合、合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11380672/

相关文章:

php - 从数据库中提取每个标签的图标

php - PHP 和 MySQL 中的评级系统

mysql - SQL JOIN 显示重复行

MySQL - 即使 InnoDB 打开时外键也不起作用 (Ubuntu 16.04)(错误代码 : 1215)

php - 使用 PHP linux 将 doc、docx、pdf 转换为 HTML

java - 为 Android 实现 Kaazing EventSource

php - 如何在 Magento 中翻译产品名称和信息

mysql - 如果当前行值为 NULL,则取上一行值

php - MYSQL 重复条目上的 AJAX 响应

c# - 如果查询未参数化,如何在查询中传递对象(字节数组)?