1

我在这里有一个查询,从 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 />";
4

2 回答 2

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')
于 2012-07-11T10:21:55.060 回答
0

这个问题在我眼里有点奇怪。如果我没有完全弄错,您所要求的只是:“使用 INSERT 语句”。

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

于 2012-07-11T10:22:54.640 回答