这是问题所在:-
我有一个名为“MatchResults”的体育比赛结果表,其中包含以下字段
Player1 Player2 Player1GamesWon Player2GamesWon Player1Pts Player2Pts League_id
首先,我正在运行一个查询,该查询将使用我已经拥有的存储变量过滤此表,从而为我提供更短的行集合:-
$qry = "SELECT * FROM BadmintonMatchResults WHERE league_id = '$leagueid'";
我需要遍历上述查询生成的每一行,并使用这些值来更新另一个名为“LeagueTable”的表。LeagueTable的结构如下:-
member_id Played Won Lost GamesWon GamesLost Difference Points
这些更新采用几个 SQL 查询的形式,例如:-
$qry = mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Played = Played + 1 WHERE member_id = '$p2id'");
if($p1pts > $p2pts) {
$qry = mysql_query("UPDATE LeagueTable SET Won = Won + 1 WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Lost = Lost + 1 WHERE member_id = '$p2id'");
}
else if($p2pts > $p1pts) {
$qry = mysql_query("UPDATE LeagueTable SET Won = Won + 1 WHERE member_id = '$p2id'");
$qry = mysql_query("UPDATE LeagueTable SET Lost = Lost + 1 WHERE member_id = '$p1id'");
}
$qry = mysql_query("UPDATE LeagueTable SET GamesWon = GamesWon + '$p1won' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesWon = GamesWon + '$p2won' WHERE member_id = '$p2id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesLost = GamesLost + '$p1won' WHERE member_id = '$p2id'");
$qry = mysql_query("UPDATE LeagueTable SET GamesLost = GamesLost + '$p2won' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Difference = GamesWon - GamesLost");
$qry = mysql_query("UPDATE LeagueTable SET Points = Points + '$p1pts' WHERE member_id = '$p1id'");
$qry = mysql_query("UPDATE LeagueTable SET Points = Points + '$p2pts' WHERE member_id = '$p2id'");
忽略我拥有的任何 $variables,因为我目前正在对表单中输入的数据执行此操作,但想要更改它以便它使用来自另一个数据库的行。我也知道我的查询可以被压缩和组合,我的主要问题是针对不同的值集多次循环这个查询集合。
我的问题是:-
- 如何在需要运行的查询中引用第一个表中的字段?显然,仅列出字段名称“Player1”是行不通的。
- 如何遍历每一行并运行上述 2 个查询以及 8 个类似的查询。
注意:其中一些查询已经嵌套在 IF 语句中,因此任何循环都必须超出该语句。
任何帮助将非常感激!