由于我远离php的专家,这让我惊呆了,我似乎无法为它编写脚本。
让我们看看能否尽可能清楚地解释这一点。
假设我有table1和table2
Table1 = (teamid, name, round1pos, round1score, round2pos, round2score)
Table2 = (id, tournamentid, teamid, name, round1pos, round1score, round2pos, round2score...till round10pos/round10score)
当从table1复制到表2时,我想在它前面添加2个字段。 (id和tournamentid)
我面临的问题是Table1每次都有不同数量的roundxpos / roundxscore。
基本上我想做的就是一旦锦标赛结束,我想删除这张桌子。但将其中的数据复制到另一个表中以存档结果。但每场锦标赛都有不同规模的比赛。
我希望有人能理解我想要实现的目标+ _ +。
答案 0 :(得分:0)
您应该创建第三个表,从现有表中删除roundX列并使用其ID引用它们。
rounds: team_id, tournament_id, no, pos, score
答案 1 :(得分:0)
你应该规范你的桌子,但同时...... 我假设主要问题是在代码中处理可变数量的roundxpos和roundxscore列。
希望这会有所帮助:
通过循环字段名称
创建table2插入//example, assuming results are fetched into an associative array
$query = "INSERT INTO table2 (tournamentid, teamid, name)";
foreach($roundpos_fields as $f){ $query .= "," . $f['Field']; }
foreach($roundscore_fields as $f){ $query .= "," . $f['Field']; }
$query .= "( SELECT $tournamentid, '' teamid, name";
foreach($roundpos_fields as $f){ $query .= "," . $f['Field']; }
foreach($roundscore_fields as $f){ $query .= "," . $f['Field']; }
$query .= ")";