简体   繁体   中英

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.

Lets see if i can explain this as clearly as possible.

Lets say i have table1 and 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. (id and tournamentid)

The problem i am facing is that Table1 has a different amount of roundxpos/roundxscore each time.

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.

Hope this helps:

  • Get the results of this query: SHOW COLUMNS FROM Table1 WHERE Field LIKE 'round%pos'
  • Get the results of this query: SHOW COLUMNS FROM Table1 WHERE Field LIKE 'round%score'
  • Create the table2 inserts by looping through field names

     //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 .= ")"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM