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

The Archive Base Latest Questions

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

Generally when I need to insert/update multiple rows, I will generate (in PHP) a

  • 0

Generally when I need to insert/update multiple rows, I will generate (in PHP) a statement like the following:

INSERT INTO Products (ProductID, Name, Weight, Color) VALUES 
(1001, "Product A", 14.5, "Red"),
(1002, "Product B", 12.3, "Green"),
(null, "Product C", 15.3, "Yellow"),
...
(null, "Product Z", 10.4, "Blue")
ON DUPLICATE KEY UPDATE 
    Name = VALUES(Name), 
    Weight = VALUES(Weight), 
    Color = Values(Color)

(The table Products only has a key on ProductID, but has many more columns than those being updated here.)

I use this kind of query because it can be generated to insert/update any number of Products by iterating through the set of products to be inserted/updated.

The problem, however, is when some of the values should not be updated.

If possible, I’d like to generate a query that only updates certain columns on a per-row basis:

INSERT INTO Products (ProductID, Name, Weight, Color) VALUES 
(1001, "Product A", DO_NOT_UPDATE, "Red"),
(1002, "Product B", 12.3, DO_NOT_UPDATE),
(null, "Product C", 15.3, "Yellow"),
...
(null, "Product Z", 10.4, "Blue")
ON DUPLICATE KEY UPDATE 
    Name = VALUES(Name), 
    Weight = VALUES(Weight), 
    Color = Values(Color)

I can think of three options:

  1. Generate multiple queries that insert/update one row at a time.

  2. Generate multiple queries that update one (non-id) column at a time. First you would have to do your insert statement separately, use mysql_insert_id() to add IDs, then do one update query per column, including only the elements that need the particular column updated. Generally you would end up with as many queries as you have columns (unless you were fortunate enough that there was some overlap in the columns not to be updated).

  3. For values not needing an update, get the existing values first in a SELECT query, then simply generate the INSERT ... ON DUPLICATE KEY UPDATE query as written at the top of this post.

As far as I can tell from the mysql ‘on duplicate key’ documentation, there is no way to ignore a value on a row-by-row basis. (Obviously using NULL is just going to set the value of that row to NULL.) Is there anything I am missing?

Any thoughts as to which of these methods will be most efficient, particularly in the case where the number of rows is > 50 and the number of columns is 5-10? Also for the case when there are only 1-2 columns to be updated per row, though the columns to be updated vary by row across all the columns.

Thanks.

  • 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-28T01:29:24+00:00Added an answer on May 28, 2026 at 1:29 am

    If you don’t need NULL as a valid value for your fields, then you can do something like this:

    INSERT INTO Products (ProductID, Name, Weight, Color) VALUES 
    (1001, "Product A", NULL, "Red"),
    (1002, "Product B", 12.3, NULL),
    (null, "Product C", 15.3, "Yellow")
    ON DUPLICATE KEY UPDATE 
        Name = IF(VALUES(Name) IS NULL, Name, VALUES(Name)), 
        Weight = IF(VALUES(Weight) IS NULL, Weight, VALUES(Weight)), 
        Color = IF(VALUES(Color) IS NULL, Color, VALUES(Color));
    

    So, if you pass a NULL then UPDATE sets value of a field to itself (thus effectively ignoring the operation). Otherwise, the field is updated to the new value.

    If you do need NULL, pick another flag value.

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

Sidebar

Related Questions

I'm trying to perform an INSERT INTO statement like the following: insert into table1
I am generally not very fond of refactoring tools. No need to get into
I'm trying to do the classic Insert/Update scenario where I need to update existing
I'm trying to insert a bunch of rows into an empty UITableView in one
I am trying to run a query: INSERT INTO `ProductState` (`ProductId`, `ChangedOn`, `State`) SELECT
I'm using a prepared statement to insert some values into my table. Before doing
I generally need to do a fair amount of text processing for my research,
We're designing data import from an external source like MAS200 into our production SQL
In my .net mvc application, I need to dynamically insert contents from local files
This 2007 White Paper compares the performance for individual select/insert/delete/update and range select statements

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.