[英]MySQL join / sum value from one table colum with a count values from another
[英]Copy one table to another with different colum count
由于我距离php专家还很远,这让我感到震惊,而且我似乎无法为此编写脚本。
让我们看看我是否可以尽可能清楚地解释这一点。
可以说我有table1和table2
Table1 = (teamid, name, round1pos, round1score, round2pos, round2score)
Table2 = (id, tournamentid, teamid, name, round1pos, round1score, round2pos, round2score...till round10pos/round10score)
当从表1复制到表2时,我想在其前面添加2个字段。 (ID和锦标赛ID)
我面临的问题是Table1每次都有不同数量的roundxpos / roundxscore。
基本上,我想做的就是在比赛结束后删除表格。 但是将其中的数据复制到另一个表中以存档结果。 但是每个锦标赛可以拥有不同的回合大小。
我希望有人能理解我正在努力实现的目标。
您应该创建第三个表,从现有表中删除roundX列,并使用其ID对其进行引用。
rounds: team_id, tournament_id, no, pos, score
您应该对表进行规范化,但与此同时...。我假设主要问题是处理代码中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 .= ")";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.