在回答我的问题之前,我想让你知道我上周开始学习mysql和php,所以这可能是你最近看到的最糟糕的代码。
别再找借口了,在执行以下php脚本时,我的问题来了:
if($hdd!==0){
$sql="CREATE TEMPORARY TABLE custom AS SELECT * from `builds`;";
$conn->query($sql);
if($hdd==1){
$sql="UPDATE custom SET HDD = null;";
$conn->query($sql);
}
$conn->query("DROP PROCEDURE IF EXISTS ROWPERROW2;");
$conn->query("CREATE PROCEDURE ROWPERROW2()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 1;
DECLARE price INT DEFAULT 0;
SELECT COUNT(*) FROM custom INTO n;
SET i=1;
SET price=0;
WHILE i<=n DO
SET price = (SELECT sum(`lowestPrice`) FROM ((SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Motherboard` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Cooling` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PC_Case` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PSU` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `GPU` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Memory` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `SSD` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `HDD` from `custom` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `CPU` from `custom` Where `id`=i ))) as A);
UPDATE custom SET `TotalPrice`=price WHERE `id`=i;
SET i = i + 1;
END WHILE;
End;");
if ($conn->query("CALL ROWPERROW2();") === TRUE) {
echo "Builds Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
}
我得到以下错误:
更新记录时出错:无法重新打开表:“自定义”
“custom”是一个临时表,所以我想这就是导致问题的原因,我该如何处理它呢?我不能每次都创建一个普通表,因为我希望它在多用户环境中工作
1条答案
按热度按时间wbrvyc0a1#
找到一个工作,虽然不漂亮,但很管用。我所做的是基于原始临时表(“contents”)创建多个临时表,然后在过程中使用它们,这样每个表只使用一次,如下所示:
并更改了程序,使每个表只使用一次: