select some data from one database table and insert into another table in the same database

dayDreamer

I wanna select some column from one table (oldDataTable) and insert into another table (newDataTable). Both are in the same database. I tried. If i choose only one column it works. But if i choose more than one column nothing happens.

oldDataTable has columns like

Name | Date | Voltage | Current | Power | RMS.

I just wanna select columns like Name| Date| Power and insert it into newDataTable. I am doing this when user submit a form button. Any help please.

<?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!";
?>
```
Aswin A

The parentheses is not required in the SELECT, the following query will work.

INSERT INTO newDataTable (Name, Date, Power) 
SELECT Name, Date, Power FROM oldDataTable WHERE ID = '2' 

So your code will be:

$insertNEQuery= "INSERT INTO newDataTable (Name, Date, Power) SELECT Name, Date, Power FROM oldDataTable WHERE ID = '2' ";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PostgreSQL - copy data from one table, database, server to another table, another database, server

Bulk insert in SQL Server database from one table to another

PostgreSQL: How to copy data from one database table to another database

How to copy large number of data from one table to another in same database?

SQL Server : can't select columns of one table by filtering from another table on same database issue

Insert data from one database table to other database table in sql server

Copy from one table in a database, to an exact copy of the table on another database on the same SQL server using a trigger

How to copy data from one table to other in same database?

Move data from one table to another in the same Database

Inserting data from one table to another in a database(different servers)

How to fetch data and insert into another table in same database?

How to transfer data from one table of a database to a table in another database

Select from one database , insert to another - MySQL

Insert values of one table in a database to another table in another database

How to insert and retrive data from one table to another table in mysql database

Insert and update from table in tmp database to table in another database

Copy table data from one database to another

Copy rows of data from one table to another table in same database using pgadmin

Insert from one Database table to another Database table in ANDROID

mysql insert into table select from another database

select data from one column based on another column's condition in the same table and insert that resulting data to another table

How to insert a row from one database to the same table in other databases?

Insert specific rows from one table in database into another with different columns

Insert into select from another database table in SQL Server

How to select data from database with same query but different condition in a table?

Rails - copy a Table from one schema to another schema in same database

Is it possible to Insert data in one table and select data from another in same query in postgreSQL?

How to insert the selected data columns from one oracle database table into another oracle database table in real time

Filter data in one SQLite database table by ids from another table in the same database