mysql更新记录时出错:无法重新打开表(临时表)

afdcj2ne  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(400)

在回答我的问题之前,我想让你知道我上周开始学习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”是一个临时表,所以我想这就是导致问题的原因,我该如何处理它呢?我不能每次都创建一个普通表,因为我希望它在多用户环境中工作

wbrvyc0a

wbrvyc0a1#

找到一个工作,虽然不漂亮,但很管用。我所做的是基于原始临时表(“contents”)创建多个临时表,然后在过程中使用它们,这样每个表只使用一次,如下所示:

$conn->query(" CREATE TEMPORARY TABLE t1 AS SELECT * FROM custom;");
                 $conn->query(" CREATE TEMPORARY TABLE t2 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t3 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t4 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t5 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t6 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t7 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t8 AS SELECT * FROM custom;");
                $conn->query(" CREATE TEMPORARY TABLE t9 AS SELECT * FROM custom;");

并更改了程序,使每个表只使用一次:

$conn->query("CREATE PROCEDURE ROWPERROW2()
BEGIN
DECLARE n INT DEFAULT 0;

DECLARE i INT DEFAULT 1;
DECLARE price INT DEFAULT 0;

SELECT COUNT(*) FROM builds 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 `t1` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Cooling` from `t2` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PC_Case` from `t3` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `PSU` from `t4` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `GPU` from `t5` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `Memory` from `t6` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `SSD` from `t7` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `HDD` from `t8` Where `id`=i )) UNION (SELECT `lowestPrice` FROM products WHERE name IN (SELECT `CPU` from `t9` 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;
}

相关问题