前のクエリのすべては、「id」の部分と、roomtable の結合が欠落している可能性を除いて、儀式であると思います。product_id は、要求された ID であると思います。
私の直感は、注文テーブルの id 列が自動生成されるということです
INSERT INTO orders (product_id, qty ,price,room_number, etc)
SELECT products.id, products.qty, products.price, roomTable.room_number, ...
FROM products
Inner join roomTable
on roomtable.product_id = products.id
WHERE products.id = $id AND $qty < qty_on_hand;
私はコードが次のようになると仮定します::
begin tran orderstransaction
//This will insert a new order with an autogenerated order id..
INSERT INTO orders (product_id, qty ,price,room_number, etc)
SELECT products.id, products.qty, products.price, roomTable.room_number, ...
FROM products
Inner join roomTable
on roomtable.product_id = products.id
WHERE products.id = $id AND $qty < qty_on_hand;
//this will reduce the qty_on_hand by $qty now that an order has been posted
update products set qty_on_hand = (qty_on_hand - $qty)
where products.id = $id
commit tran orderstransaction
これが機能する少なくとも1つのステートメントを含む私の編集です
SQL Insert Statement that worked for me
..................
this sql statement below assumes that id in orders is not autogenerated..
i did not see an attribute for price in products so hardcoded it to 2500 to be replaced with products.price assuming there is a price in products table.
Now assuming that u r querying for product_id=1 and qty=250 which are ur inputs for the query, am inserting order id 10000 into orders
INSERT INTO orders (id,product_id, qty ,price,room_number)
SELECT 10000,products.id,250 , 2500, roomTable.room_number
FROM products
Inner join roomTable
on roomtable.product_id = products.id
WHERE products.id = 1 AND 250 < products.qty_on_hand
..........................................................
This Sql assumes order id is autogenerated
INSERT INTO orders (product_id, qty ,price,room_number)
SELECT products.id,250 , 2500, roomTable.room_number
FROM products
Inner join roomTable
on roomtable.product_id = products.id
WHERE products.id = 1 AND 250 < products.qty_on_hand
........................................................