user3521635
user3521635

Reputation: 1

Insert from one table into another (with extra values) (MySQL)

Had a search but can only find stuff on inserting from one table to another.

I have this query which takes data from one table and inserts it into another:

INSERT INTO tbl_orders (product_id, customer_id) 
  SELECT product_id, customer_id FROM tbl_basket 
  WHERE customer_id = '" . $_SESSION['user'] . "'");

What I want to do is this with extra data included. For example I would insert the product_id and customer_id based of the other table, and insert extra values which are not located in another table.

I have tried this with no luck:

INSERT INTO tbl_orders (product_id, customer_id, order_status) 
  SELECT product_id, customer_id, \'placed\' FROM tbl_basket 
  WHERE customer_id = '" . $_SESSION['user'] . "'"

Any ideas? Thank you.

Upvotes: 0

Views: 504

Answers (1)

Ikan
Ikan

Reputation: 71

If you want insert extra data into table, you can tried this query:

INSERT INTO tbl_orders (product_id, customer_id, order_status) 
SELECT product_id, customer_id, 'order_status' FROM tbl_basket 
WHERE customer_id = 'example'

If you have extra data in second table, you can join the second table like this:

INSERT INTO tbl_orders (product_id, customer_id, order_status) 
SELECT product_id, customer_id, t2.order_status FROM tbl_basket t1 
join tbl_basket2 t2 on t1.id = t2.id
WHERE t1.customer_id = 'example'

Upvotes: 1

Related Questions