简体   繁体   English

将一张表复制到另一张具有不同列数的表

[英]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: 希望这可以帮助:

  • Get the results of this query: SHOW COLUMNS FROM Table1 WHERE Field LIKE 'round%pos' 获取此查询的结果:从表1的SHOW COLUMNS字段喜欢'round%pos'
  • Get the results of this query: SHOW COLUMNS FROM Table1 WHERE Field LIKE 'round%score' 获取此查询的结果:从表1的SHOW COLUMNS字段喜欢'round%score'
  • 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.

相关问题 MySQL联接/求和来自一个表列的值与来自另一个表的计数值 - MySQL join / sum value from one table colum with a count values from another 从一个表复制到另一个具有相同列数和名称的表 - copy from one table to another with the same column count and name MySql 获取其中一列等于另一列等于另一表中的另一列的行 - MySql get rows where one column equals to another colum in another table where one column is equal 在laravel 5中将一张表复制到另一张表 - Copy one table to another in laravel 5 如何将一个表复制到另一个字段中的另一个表并添加日期(table2没有日期时间) - how do I copy one table to another in different field AND add date( table2 dont have datetime) 从一个表复制所有信息,然后使用PHP将其插入到不同服务器上的另一表中 - Copy all information from one table and insert it into another table on different server using PHP 将一行从一张表复制到另一张表 - Copy one row from one table to another 如何将具有不同列数的行从一个表复制到另一表 - How can I copy rows from one to another table with a different number of columns 将数据从一个 sql 表复制到另一个具有较少列数和不同列的表 - copy data from one sql table to another having less number of columns and a different column 无法将数据从一个表复制到另一个表 - Not able to copy data from one table to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM