从PHP更新MYSQL中的语句

从PHP更新MYSQL中的语句

我有一张桌子,如下所示


该表的输出
绿色是宣布获胜者的地方,而黄色则表示他们已提交报告,但尚未决定获奖者。 (优胜者字段未填充)

        $roundBound = Array();
        $query = "SELECT MIN(id), MAX(id) FROM $tablename GROUP BY round";
        if($result = $Login->mysqli->query($query)) {
            while ($row = $result->fetch_row()) {
                $roundBound[] = $row[0];
                $roundBound[] = $row[1];
            }
        }
        for($i = 0; $i < count($roundBound); $i = $i + 2) {
            $match = 0;
            for($j = $roundBound[$i]; $j < $roundBound[$i + 1]; $j = $j + 2) {
                $id1 = $j;
                $id2 = $j+1;
                $query = "SELECT t1.winner, t2.winner FROM $tablename as t1, $tablename as t2 WHERE t1.id=$id1 AND t2.id=$id2";
                $result = $Login->mysqli->query($query);
                $row = $result->fetch_row();
                $win1 = $row[0];
                $win2 = $row[1];
                if (isset($win1) && isset($win2)) {
                    if (isset($roundBound[$i + 2])) {
                        $id = $roundBound[$i + 2] + $match;
                        $query = "UPDATE $tablename SET
                            username = '$win1',
                            username2 = '$win2'
                            WHERE id = $id";
                        $Login->mysqli->query($query);
                    } else {
                        //Tourneydonneee yeeee
                    }
                }
                $match++;
            }
        }


有没有人有改进或全方位的想法?这将经常运行,并且可能会在大型记录集上运行,因此也将欢迎优化。

最佳答案

据我了解您的问题,我可以建议您尝试

for ($i = 0; $i < count($arr); $i = $i + 2) {
    $win1 = mysql_query("SELECT winner FROM tableName WHERE id=".(int)$i);
    $win2 = mysql_query("SELECT winner FROM tableName WHERE id=".(int)$i+1);
    $qry = "INSERT INTO tableName SET
                          username = $win1,
                          username2 =$win2,
                          ....";
    mysql_query($qry);
}

关于php - 从PHP更新MYSQL中的语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6850537/

10-16 07:19