简体   繁体   中英

Save pulled data from MySQL table into another table

I have a query here pulling data from MySQL database. It's working great.But what am trying to do is save all the data pulled from the module table into another table. How could I go about this please ?? Any help would be appreciated. Thanks.

$coremoduleY1Query = "SELECT ID,title,credits FROM module WHERE ID = 'CS1101'
            UNION SELECT ID,title,credits FROM module WHERE ID = 'CS1105'
            UNION SELECT ID,title,credits FROM module WHERE ID = 'CS1106'
            UNION SELECT ID,title,credits FROM module WHERE ID = 'CS1107'
            UNION SELECT ID,title,credits FROM module WHERE ID = 'CS1108'
            UNION SELECT ID,title,credits FROM module WHERE ID = 'CS1109'";                                                                           


        $coremoduleY1Result = mysql_query($coremoduleY1Query);
        echo "<B> Core Modules </B>";                      
        while ($coremoduleRow = mysql_fetch_array($coremoduleY1Result)) {

                 $id = htmlentities($coremoduleRow['ID']);
                 $title = htmlentities($coremoduleRow['title']);
                 $credits = $coremoduleRow['credits'];

                 echo "<ul>" . $id . " " . $title .  " " . $credits . "</ul>";
       }   
       echo "<br />";

For one why are you not querying like this:

SELECT ID,title,credits FROM module WHERE ID IN ('CS1101','CS1105','CS1106','CS1107','CS1108','CS1109')

And secondly you can do what you want totally in MySQL:

INSERT INTO other_table (ID,title,credits)
SELECT ID,title,credits FROM module WHERE ID IN ('CS1101','CS1105','CS1106','CS1107','CS1108','CS1109')

That question is a little strange in my eyes. If I am not totally mistaken all you ask for is: "use an INSERT statement".

You might want to shorten this into a single statement: INSERT INTO ... (col1,col2,...) (SELECT ... FROM module);

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