简体   繁体   中英

Autopopulating a Mysql Table from Another Mysql Table with iterations through PHP

The explanation of this may seem a bit long and convoluted but please bear with me. In essence what I want to do is fill a mysql table(A) from another mysql table(B) in my database but in order to do so I need to duplicate values in table (A) so that there will be enough entries to accomodate for the values in table B.

Now for a more concrete example

How the tables look

course_details table


+------------------+-----------+-----------+------------+
        1               1       To be Set       36
        2               2       To be Set       54
        3               3       To be Set       78
        4               4       To be Set       23

year table


+-----------------+------+---------+
         1          2012      1
         2          2012      2
         3          2012      3
         4          2012      4
         5          2013      1
         6          2013      2
         7          2013      3
         8          2013      4

How I want the table to look


-------------------+-----------+---------+------------+
         1               1          1          36
         2               1          2          36
         3               1          3          36
         4               1          4          36
         5               1          5          36
         6               1          6          58
         7               1          7          36 
         8               1          8          47
         9               2          1          54
        10               2          2          54
        11               2          3          54
        12               2          4          67
        13               2          5          67
        14               2          6          54
        15               2          7          54
        16               2          8          54

How the code looks

<?php
require_once('open_db.php');
get_dbhandle();

$query_year = "SELECT * FROM year"; 
$result_year = mysql_query($query_year) or die(mysql_error());
$num_year_rows = mysql_num_rows($result_year);
$num_year_rows = ($num_year_rows - 1);

$query_yearid = "SELECT year_semester_id FROM year"; 
$result_yearid = mysql_query($query_yearid) or die(mysql_error());

$result_ccheck = mysql_query("SELECT course_id FROM courses");
while($row = mysql_fetch_array($result_ccheck))
    {
      $course_id = $row['course_id'];

       for($i = $num_year_rows; $i >= 0; $i--)
       {
        $query_cdetails = "INSERT INTO course_details (course_id) VALUES ('$course_id')";
        $result_cdetails = mysql_query($query_cdetails);

           while($row = mysql_fetch_array($result_yearid))
           {
             $year_semester_id = $row['year_semester_id'];
             $query = "INSERT INTO course_details(year_semester_id) SELECT year_semester_id FROM year";
             $result = mysql_query($query);
            }

        }      
    }
?>

What it does vs what I want it to do: As it currently is set, it correctly creates duplicates of each course_id in course_details table to match the number of year_semester_id's which exist in the years table which is perfect. The problem comes to inserting the year_semester_id's in each corresponding table slot of the table course_details.

In other words, to ensure that when course_id =1 , year_semester_id=1, course_id=1, year_semester_id =2,....course_id=1, year_semester_id=8, course_id=2, year_semester_id=1, course_id=2, year_semester_id=2......course_id=2, year semester_id =8, course_id=3, year_semester_id =1 etc and so on.... Therein lies the issue.

A recap of how the code works, it counts the number of year_semester_id's in the years table, it then subtracts that number by 1 which is the amount of times the course_id is currently in the course_details table and it duplicates it by that number. This total number (the duplicates) plus the original course_id should be the total amount of year_semester_ids. I now want to insert every year_semester_id for every course_id that exists and loop through until each course_id is accounted for. Thank you

It looks to me like what you're attempting to do could easily be done without bloating your database by taking advantage of relational tables. In this case, if I'm understanding you correctly, the end result here is you want to have duplicates of all the rows from course_details with the empty column set to each of the rows from the year table.

That being true, you could select that data using JOIN statements:

SELECT `a`.`course_id` , `b`.`year_semester_id` as `year_id` , `a`.`teacher_id` FROM `course_details` `a` INNER JOIN `year` `b`

That should return the data you want in a MySQL resultset. If you want to insert that data into a table, just make sure the table has the correct columns, and set the course_Details_id field to auto increment and do:

INSERT INTO `tablename` ( `course_id` , `year_semester_id` , `year_id` ) VALUES (
    SELECT `a`.`course_details_id` , `a`.`course_id` , `b`.`year_semester_id` as `year_id` , `a`.`teacher_id` FROM `course_details` `a` INNER JOIN `year` `b`
)

This should insert all the data you need into the new MySQL table without the need for PHP scripts.

这就是我所说的检查代码,即您在理解上有困难,请告诉我。

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