3

I am trying to "syphon" data over from one table to another. The problem I run into is that some of the data which is going in the new table is static, other data is being copied from an existing table. Let me start by showing the query I was trying to run:

INSERT INTO current_cart 
    (cart_ID, 
    cart_PO, 
    cart_user, 
    cart_date, 
    cart_qty, 
    cart_sku, 
    cart_description, 
    cart_price, 
    cart_linetotal) 
VALUES 
    ('$cartID', 
    '$poNumberNew', 
    '$email', 
    '$lineDate',
SELECT 
    orderdetail_qty, 
    orderdetail_sku, 
    orderdetail_description, 
    orderdetail_price, 
    orderdetail_linetotal
FROM orderdetail
WHERE orderdetail_custemail = $email AND orderdetail_po = $poNumber)

Obviously, all the PHP variables are declared beforehand. Essentially what I have running is a shopping cart. This query would take the items from a previous order and enter them in a new shopping cart so our customer can start a new order based on a previous order.

The problem I run into is how to insert records into a table when some of the data for the record is static (cartID, poNumberNew, email and lineDate) and other information is coming out of a different table? I am hoping I can do this without creating a loop to repeat a query for however many items there were in the order the customer is duplicating... I think that would bog down our site significantly.

I've seen a myriad of awesome answers on many other web dev questions I've had in the past, I'm hoping the stackoverflow community can help me out here...

Thanks!

1 Answer 1

3

in a SELECT statement, you can have a column that returns an constant. For example,

INSERT INTO current_cart 
    (cart_ID, 
    cart_PO, 
    cart_user, 
    cart_date, 
    cart_qty, 
    cart_sku, 
    cart_description, 
    cart_price, 
    cart_linetotal) 

SELECT 
    '$cartID', 
    '$poNumberNew', 
    '$email', 
    '$lineDate',
    orderdetail_qty, 
    orderdetail_sku, 
    orderdetail_description, 
    orderdetail_price, 
    orderdetail_linetotal
FROM orderdetail
WHERE orderdetail_custemail = $email AND orderdetail_po = $poNumber)
Sign up to request clarification or add additional context in comments.

2 Comments

Wow... so simple... and so effective! Thanks, TheVedge!
Anytime! It also works in insert, delete, update, and wherever you would usually have a column name, really.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.