MYSQLI Prepared statement 更新语句where in array

MYSQLI Prepared statement update statement with where in array

我设法创建了一个准备好的语句来更新 SQL 数据库中的硬编码值 table 使用 mysqli 准备好的语句和包含数组的 Where IN 语句..

我需要根据变量 table 中获取的名称,用 table 帐户中的值 $arrayresetid 更新 table resetstatsstats_accid 列=17=] 存储在 table resetstatsusername 列中。

这是我的代码:

if ($totalacc > 0) {
    $in7 = str_repeat('?,', count($arrayidc) - 1) . '?';
    $types7 = str_repeat('s', count($arrayidc));
    $sql7 = "SELECT email,name,group_name,id FROM account WHERE name IN ($in7) AND NOT group_name = 'test'";
    $stmt7 = $mysqli->prepare($sql7);
    $stmt7->bind_param($types7, ...$arrayidc);
    $stmt7->execute();
    $result7 = $stmt7->get_result();
    while ($rowid7 = $result7->fetch_assoc()) {
        $arrayresetid = $rowid7['id'];
        $arrayresetname = $rowid7['name'];
        $arrayresetnamereplace = str_replace(" (resetted)", "", $arrayresetname);
        $arraynamereset[] = $arrayresetnamereplace;
    }

    if ($stmt7 == true) {
        $in9 = str_repeat('?,', count($arraynamereset) - 1) . '?';
        $sql9 = "UPDATE resetstats SET statsfriendly = 1,stats_accid = ? WHERE username IN ($in9) ";
        $stmt9 = $mysqli->prepare($sql9);
        $arraynamereset[] = $arrayresetid;
        $stmt9->bind_param(str_repeat('s', count($arraynamereset)), ...$arraynamereset);
        $stmt9->execute();
        $stmt9->close();
    }
}

示例数据table 账户:

id | name
865 | test name a
876 | test name b
888 | test name c

示例数据table resetstats(更新前!):

statsid | username | statsfriendly | stats_accid
2 | test name a | 0 | 0
3 | test name b | 0 | 0
4 | test name c | 0 | 0

示例数据table resetstats(更新后的预期输出!):

statsid | username | statsfriendly | stats_accid
2 | test name a | 1 | 865 
3 | test name b | 1 | 876 
4 | test name c | 1 | 888

您应该使用单个查询来执行此操作。

UPDATE resetstats AS r
JOIN account AS a ON r.username = REPLACE(a.name, ' (resetted)', '')
SET r.statsfriendly = 1, r.stats_id = a.id
WHERE a.name IN ($in7)