我想从一个表(oldDataTable
)中选择某列,然后插入到另一表(newDataTable
)中。两者都在同一个数据库中。我试过了。如果我只选择一列,它会起作用。但是,如果我选择多于一列,则什么也不会发生。
oldDataTable的列类似
Name | Date | Voltage | Current | Power | RMS.
我只想选择Name| Date| Power
之类的列并将其插入newDataTable
中。用户提交表单按钮时,我正在执行此操作。请帮忙。
<?php
if (isset($_POST['submit'])){
include ("DBconnect.php");
$conn= mysqli_connect( $dbhost, $dbuser, $dbpass, $db );
if ($conn->connect_error){
die("Connection failed: " . $conn->connect_error);
}
else{
$insertNEQuery= "INSERT INTO newDataTable (Name, Date, Power) SELECT (Name, Date, Power) FROM oldDataTable WHERE ID = '2' ";
//it should be a bunch of rows. I have data for every one minute.
mysqli_query($conn, $insertNEQuery);
echo " data updated!";
$conn->close();
}
} else
echo " please click submit button!";
?>
```
答案 0 :(得分:2)
SELECT
中不需要括号,下面的查询将起作用。
INSERT INTO newDataTable (Name, Date, Power)
SELECT Name, Date, Power FROM oldDataTable WHERE ID = '2'
所以您的代码将是:
$insertNEQuery= "INSERT INTO newDataTable (Name, Date, Power) SELECT Name, Date, Power FROM oldDataTable WHERE ID = '2' ";
答案 1 :(得分:0)
使用此查询
INSERT INTO newDataTable (SELECT Name, Date, Power FROM oldDataTable WHERE ID = '2')";