mysql if insert,MySQL INSERT IF(自定义if语句)

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.

My question is: can I do this in my query?

Now, why would I want to do that?

Let's assume we have the following 2 tables:

products: id, qty_on_hand

orders: id, product_id, qty

Now, let's say an order for 20 Voodoo Dolls (product id 2) comes in.

We first check if there's enough Quantity On Hand:

SELECT IF(

( SELECT SUM(qty) FROM orders WHERE product_id = 2 ) + 20

<=

( SELECT qty_on_hand FROM products WHERE id = 2)

, 'true', 'false');

Then, if it evaluates to true, we run an INSERT query.

So far so good.

However, there's a problem with concurrency.

If 2 orders come in at the exact same time, they might both read the quantity-on-hand before any one of them has entered the order.

They'll then both place the order, thus exceeding the qty_on_hand.

So, back to the root of the question:

Is it possible to run an INSERT statement conditionally, so that we can combine both these queries into one?

I searched around a lot, and the only type of conditional INSERT statement that I could find was ON DUPLICATE KEY, which obviously does not apply here.

解决方案INSERT INTO TABLE

SELECT value_for_column1, value_for_column2, ...

FROM wherever

WHERE your_special_condition

If no rows are returned from the select (because your special condition is false) no insert happens.

Using your schema from question (assuming your id column is auto_increment):

insert into orders (product_id, qty)

select 2, 20

where (SELECT qty_on_hand FROM products WHERE id = 2) > 20;

This will insert no rows if there's not enough stock on hand, otherwise it will create the order row.

Nice idea btw!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值