tags:

views:

46

answers:

2

is it possible to copy data from a table to another table?.

here is my example. i have two tables, warehouse and showroom

my warehouse table has (product_id and stock_quantity) and my showroom table has (product_id(fk from warehouse), stock_transferred )..

for example in warehouse table

product_id     stock_quantity
1              10
2              20

how can I transfer product_id(1)with stock_quantity(5) in showroom table and still retain the data in warehouse table?

so after transferring data my warehouse table becomes these:

product_id     stock_quantity
1              5
2              20

and my showroom table becomes these:

product_id     stock_transferred
1              5

how can i do these in a php form?for example i have a text input in which the user can specify how many stocks he will transfer to showroom table.

sorry I cant explain well Im not good in english.

A: 

You can use insert into(select...)

GôTô
A: 
BEGIN TRAN 

INSERT INTO showroom
SELECT product_id, @ValueToBeReduced FROM warehouse 
WHERE product_id = 1

-- error handling

UPDATE warehouse 
SET stock_quantity = stock_quantity - @ValueToBeReduced
INNER JOIN showroom
ON warehouse.product_id = showroom.product_id
AND showroom.product_id = 1

-- error handling

COMMIT TRAN
InSane
can you add the php form?
kester martinez
There is no 'PHP form'. You pass the above SQL to the appropriate DB functions from your PHP.
Colin Fine
i mean how can i implement these together with a php form?. for example users have a form in which they can choose what products and how many stocks they can transfer to showroom.
kester martinez
what do you mean with '@ValueToBeReduced'?.. is it a column?
kester martinez
@kester martinez - @ValueToBeReduced - is the value you want to reduce the stock quantity by. Ideally this value would be entered in by a user from the UI. This value will be passed as a parameter to the SQL which uses it both to reduce the warehouse entry and insert entry into the showroom table
InSane