Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:12:24+00:00 2026-05-24T01:12:24+00:00

First, here’s the concise summary of the question: Is it possible to run an

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:12:25+00:00Added an answer on May 24, 2026 at 1:12 am
    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first here in Stackoverflow. So I just want to ask question
first question here, this is regarding the iPhoneOS3 not MacOSX. I am fairly new
First question here and can I start by saying how much help this site
First question here, and yes this is a homework question. We are tasked with
First here's what I'm using and trying to do: the minimal setup for this
First question here: it is a very short yet fundamental thing in Java that
First question here so hello everyone. The requirement I'm working on is a small
First off, code-readability goes out the window for this question. I'm all for code
first question here, be nice :P I am trying to make an infinite UIScrollView
I will try and explain this without causing any confusion, first here is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.