phpmysql

How to copy column from one table data to another table and insert more columns same time?


I want to know, how to copy a column from one table data to another table in MySQL and insert more values at the same time.

Table 1:

**id:** 1
**Items :** [{"id":"9","size":"64 GB","quantity":"1"},{"id":"9","size":"32 GB","quantity":2},{"id":"1","size":"32","quantity":"1"},{"id":"5","size":"8","quantity":"1"},{"id":"2","size":"38","quantity":"1"}]
**Create_Date:** 31/08/2018
**Exp_date:** 01/09/2018

Table 2:

**id:**
**Order_id:**
**Items:**
**Paid:**

I want to copy table 1 items and insert it table 2 and at the same time I want to insert order_id, paid in table 2

For this I am trying this:

$order_id = "DJ9JQY4J00248";
$paid = "Order Complete";
$insertSql = "INSERT INTO table2  (`order_id`,`paid`) VALUES ('$order_id','$paid') AND (items) SELECT items FROM cart WHERE id = '{$cart_id}'";
$db->query($insertSql);

Kindly suggest what I am doing wrong. The above Code is not working for me.


Solution

  • You can move your variables to select

    INSERT INTO table2  
        (`order_id`,`paid`, items)  
        SELECT '$order_id','$paid', items FROM cart WHERE id = '{$cart_id}'