我正在为 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/