Home > Software engineering >  SQL how do I INSERT INTO a tables from other seperate tables
SQL how do I INSERT INTO a tables from other seperate tables

Time:03-08

I want to insert values into a table ,with values coming from separate tables as well as a php file,but i also need there to be a where clause. I have no idea how to do this so any help would be appriated.

The current SQL to get an idea whats going on is:

INSERT INTO order1(Order_ID,Supplier_ID,Product_ID,Stock_Amount,Reorder_Time,Postcode Amount_Ordered,Telephone_Number) VALUES(product.product_ID,:supplier_ID,product.Stock_Amount,:stockamount,supplier.Reorder_Time, supplier.Postcode,:Amount_Ordered,supplier.Telephone_Number) WHERE product.product_ID=:product_ID

(:Variable is just some data being passed into by a php file and product and supplier are just other tables)

The code doesnt need to be efficient it just needs to work, Any help is very much appricitated

CodePudding user response:

You could phrase this as an INSERT INTO ... SELECT:

INSERT INTO order1 (Order_ID, Supplier_ID, Product_ID, Stock_Amount,
                    Reorder_Time, Postcode, Amount_Ordered, Telephone_Number)

SELECT product_ID, :supplier_ID, Stock_Amount, :stockamount, Reorder_Time, Postcode,
       :Amount_Ordered, Telephone_Number
FROM order1
WHERE product_ID = :product_ID;
  • Related