Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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!";
?>
```
like image 638
dragon123 Avatar asked Jan 25 '26 08:01

dragon123


1 Answers

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' ";
like image 72
Aswin A Avatar answered Jan 27 '26 23:01

Aswin A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!