0

If 条件が true を返す場合、複数の値をテーブルに挿入します。if 条件は 2 つのテーブル データをチェックし、それに応じて true と false を返します。

構造-

products: id, qty_on_hand
orders: id, product_id, qty ,price,room_number etc
roomTable :id,product_id,room_number,booked_status

要求された合計数量が qty_on_hand より小さい場合、orders テーブルにすべてのデータを挿入します

case ステートメントがこの中でどのように使用されているか教えてください。事前に感謝します

4

2 に答える 2

1
INSERT INTO orders (id, product_id, qty ,price,room_number, etc)
SELECT $id, $product_id, $qty, $price, $room_number, ...
FROM products
WHERE id = $id AND $qty < qty_on_hand;

$xxxすべてを挿入するデータに置き換えます。

于 2013-01-26T07:22:21.510 に答える
0

前のクエリのすべては、「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
........................................................
于 2013-01-26T08:38:22.283 に答える