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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:47:19+00:00 2026-06-07T01:47:19+00:00

I am trying to update 3 column values in a row in mysql only

  • 0

I am trying to update 3 column values in a row in mysql only if any of the 3 values is different.

Say I have a table of

x,y,z,id columns

I have currently,

Method A

update foo set x = 'x_value', y = 'y_value', z = 'z_value' where id = 'unique_id'
and ((x <> 'x_value') or (y <> 'y_value') or (z <> 'z_value'))

I don’t know much about the theoretical benchmarking/architecture of mysql, and I was wondering if the statements

Method B

update foo set x ='x_value' where id = 'unique_id' and ((x <> 'x_value')); 
update foo set y ='y_value' where id = 'unique_id' and ((y <> 'y_value')); 
update foo set z ='z_value' where id = 'unique_id' and ((z <> 'z_value')); 

is better or superior.

I realize that Method B will only do one write and 3 reads if only one column has changed, vs 3 writes and 3 reads for the Method A. I just don’t know if it is more time intensive because method B requires looking up the index row 3 times.

Thanks in advance!

  • 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-06-07T01:47:21+00:00Added an answer on June 7, 2026 at 1:47 am

    Based on what I’ve read in the comments, I agree with octern that you should simply run an update. It will use significantly less resources and based on your table engine, it will free up your table/ row lock for less time, making your table perform a lot better.

    However, if you insist on doing a check before doing a write, do so through PHP. Simply do a select statement, compare the code in PHP and then update the appropriate table(s). For example:

    $res = mysql_query("SELECT * FROM table1 WHERE PK_ID = '0'");
    $arr = mysq_fetch_assoc($res);
    $update = false;
    if ($arr["field_1"] != $_POST["field_1"])
    {
        $update = true;
    }
    
    if ($arr["field_2"] != $_POST["field_2"])
    {
        $update = true;
    }
    
    if ($update)
    {
        mysql_query(sprintf("UPDATE table1 SET field_1 = '%s', field_2 = '%s'", $_POST["field_1"], $_POST["field_2"]));
    }
    if (
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dynamic table with each row having 4 columns. Column 1 has
I am trying to update values in a column like the following code block
I'm trying to update a column to a max value, but only when grouped
I am trying to update a column in my table which was last inserted.
I'm trying to UPDATE a column for a user, so that they can only
I'm trying to update the comments column in one table (Entries), when a new
Let's say I have a MySQL database with 3 tables: table 1: Persons, with
I'm trying to determine what situations MySQL updates an index. Say I have the
Since it's friday my brain must have malfunctioned. I'm trying to update a column
I'm trying to update some rows in a table based on two other columns

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.