将拉出的数据从MySQL表保存到另一个表中

时间:2012-07-11 10:18:59

标签: php mysql

我在这里从MySQL数据库中提取数据。它工作得很好。但是我要做的是将从模块表中提取的所有数据保存到另一个表中。我怎么能这样呢?任何帮助,将不胜感激。感谢。

$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 />";

2 个答案:

答案 0 :(得分:1)

对于你为什么不这样查询:

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

其次,您可以在MySQL中完成您想要的任务:

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

答案 1 :(得分:0)

这个问题在我看来有点奇怪。 如果我没有完全错误,你要求的是:“使用INSERT语句”。

您可能希望将其缩短为单个语句: INSERT INTO ...(col1,col2,...)(SELECT ... FROM module);