First, here's the concise summary of the question:
Is it possible to run an INSERT statement conditionally? Something akin to this:
IF(expression) INSERT...
Now, I know I can do this with a stored procedure.
Now, I want to do that
Let's assume we have the following 3 tables:
products: id, qty_on_hand
orders: id, product_id, qty
roomTable :id,product_id,room_number,booked_status
Now, let's say an order for 20 rooms (product id 2) comes in. We first check total qty of given product_id from orders table as total_qty and total_qty < qty_on_hand if it return true then insert order values and update 1 room after last booked_status='Y' room from roomTable with booked_status ='Y' and give the id of that room .
Problem is that I am not able to make the join query . what i done so far -
INSERT INTO orders(product_id, qty)
SELECT 2, 20 FROM products WHERE id = 2 AND qty_on_hand >= 20