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 7559889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:45:13+00:00 2026-05-30T12:45:13+00:00

I read this question , which highlights a solution to conditionally insert values into

  • 0

I read this question, which highlights a solution to conditionally insert values into a table if they don’t already exist. My question: is it possible to conditionally insert multiple values at once.

For instance, say I have a table that just contains user names (this is a pointless table, but let’s keep it simple). The table’s contents look like this:

matthew  20
mark     24
luke     25
john     56
buddy    68

A user enters jimmy 34, mark 25 and bobby 54 in a web form and submits, and I’d like to check whether those three values exist in the table already and insert the ones that don’t in one statement. Yes, for this example, I’m assuming names are unique.

Here is a paraphrase of the code snippet from the question I linked to, adapted to this example:

INSERT INTO users(name) 
SELECT 'jimmy'
    FROM dual
    WHERE NOT EXISTS (SELECT * FROM users
                         WHERE user = 'jimmy')

How can I adapt this for multiple values being inserted at once? It’s also important that the solution work independently of the number of values entered. In my example, I give three (jimmy, mark and bobby) but there may only be one or there may be 20.

Second question: is this wise? I know that reducing the number of queries is desirable but is it worth it here? Should I just set up a for loop and loop through, alternately checking if a value exists and inserting if it doesn’t?

Thanks for any help.

Sorry I don’t have code that I’ve tried myself to show, I’m not even sure what to try here.

Update: added an extra column to the table. I wanted to keep things simple but need two columns to illustrate the fact that deleting a row and then inserting or updating are not what I want as they would favor the user’s input over what is already in the table.

  • 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-30T12:45:14+00:00Added an answer on May 30, 2026 at 12:45 pm

    See INSERT IGNORE combined with a UNIQUE KEY on the field in question.

    When a unique key constraint fails the whole row is silently ignored.

    ALTER TABLE users ADD UNIQUE KEY `name` (`name`);
    INSERT IGNORE INTO `users` (`name`, `age`) 
      VALUES ("jimmy", 22), ("bob", 45), 
             ("luke", 300), ("john", 456);
    

    Note: this will also suppress errors when datatypes mismatch and accurate conversion is impossible. (e.g. DECIMAL vs INT) MySQL will continue using the nearest result possible. (e.g. INT) You should ensure only pre-validated data is inserted with such statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently read this question which had a solution about labeling loops in Java.
I was reading this question (which you do not have to read because I
I know abstract fields do not exist in java. I also read this question
Following this question which asks (and answers) how to read .mat files that were
Slightly related to this question , but you don't really need to read that.
I have a previous question which I have provided my solution; however, I don't
I just read this question , which raises for me another question: Consider this
Having already read this question I'm reasonably certain that a given process using floating
I've read this post: ( NSDateFormatter leak question ) which states that NSDateFormatter leaks
Update: Please read this question in the context of design principles, elegance, expression of

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.