[英]Copy one table to another with different colum count
As i am far from an expert in php this got me stunned and i can't seem to make a script for it. 由于我距离php专家还很远,这让我感到震惊,而且我似乎无法为此编写脚本。
Lets see if i can explain this as clearly as possible. 让我们看看我是否可以尽可能清楚地解释这一点。
Lets say i have table1 and table2 可以说我有table1和table2
Table1 = (teamid, name, round1pos, round1score, round2pos, round2score)
Table2 = (id, tournamentid, teamid, name, round1pos, round1score, round2pos, round2score...till round10pos/round10score)
When copied from table1 to table 2 i want to add 2 fields in front of it. 当从表1复制到表2时,我想在其前面添加2个字段。 (id and tournamentid) (ID和锦标赛ID)
The problem i am facing is that Table1 has a different amount of roundxpos/roundxscore each time. 我面临的问题是Table1每次都有不同数量的roundxpos / roundxscore。
Basically what i want to do is once a tournament is over i want to delete the table. 基本上,我想做的就是在比赛结束后删除表格。 but copy the data inside it to another table to archive the results. 但是将其中的数据复制到另一个表中以存档结果。 but each tournament can have a different size of rounds. 但是每个锦标赛可以拥有不同的回合大小。
I hope someone can understand what i am trying to achieve +_+. 我希望有人能理解我正在努力实现的目标。
您应该创建第三个表,从现有表中删除roundX列,并使用其ID对其进行引用。
rounds: team_id, tournament_id, no, pos, score
You should normalize your tables but in the meantime.... I'm assuming the main problem is handling the variable number of roundxpos and roundxscore columns in the code. 您应该对表进行规范化,但与此同时...。我假设主要问题是处理代码中roundxpos和roundxscore列的变量数量。
Hope this helps: 希望这可以帮助:
Create the table2 inserts by looping through field names 通过遍历字段名称来创建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 .= ")";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.