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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:43:50+00:00 2026-05-24T03:43:50+00:00

I have two tables a source and a destination. I want to flag the

  • 0

I have two tables a source and a destination. I want to flag the destination table to be updated if any of the corresponding columns in source columns are different. The columns can be null.

Currently this seems very clumsy:

UPDATE destination d
JOIN source s
   ON d.id = s.id
SET d.updateFlag = 1
WHERE (    (d.col1 IS NULL AND s.col1 IS NOT NULL) 
        OR (d.col1 IS NOT NULL AND s.col1 IS NULL)
        OR (d.col1 <> s.col1)
      )
      OR
      (    (d.col2 IS NULL AND s.col2 IS NOT NULL) 
        OR (d.col2 IS NOT NULL AND s.col2 IS NULL)
        OR (d.col2 <> s.col2)
      )
      ...etc...
      OR
      (    (d.colN IS NULL AND s.colN IS NOT NULL) 
        OR (d.colN IS NOT NULL AND s.colN IS NULL)
        OR (d.colN <> s.colN)
      )

Ideally I could do something like:

UPDATE destination d
JOIN source s
   ON d.id = s.id
SET d.updateFlag = 1
WHERE HASH(d.col1,d.col2,...etc...,d.colN) <> HASH(s.col1,s.col2,...etc...,s.colN)

Some additional info. The columns are all different data types (some ints, some bits, some strings) and we are using a flavor of MySQL 5.1.

  • 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-24T03:43:51+00:00Added an answer on May 24, 2026 at 3:43 am

    You can simplify your statement by using the NULL-safe equal to operator:

    UPDATE destination d
    JOIN source s
       ON d.id = s.id
    SET d.updateFlag = 1
    WHERE !(d.col1 <=> s.col1)
       OR !(d.col2 <=> s.col2)
    ...etc
    

    As an alternative, you can use a UNION to find duplicate rows:

      SELECT tbl, id, col1, col2, col3
        FROM (
      SELECT 't1' AS tbl, id, col1, col2, col3
        FROM t1
       UNION ALL
      SELECT 't2' AS tbl, id, col1, col2, col3
        FROM t2
           ) tmp
    GROUP BY id, col1, col2, col3 HAVING COUNT(*) = 1;
    

    You can then use the result from that in your update query:

    UPDATE destination d
       SET d.updateFlag = 1
     WHERE EXISTS (
        SELECT NULL FROM (
            SELECT id, col1, col2, col3 FROM t1
            UNION All
            SELECT id, col1, col2, col3 FROM t2
         ) tmp
         WHERE d.id = tmp.id
      GROUP BY id, col1, col2, col3
        HAVING COUNT(*) = 1
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simplified, I have two tables Source and Target. declare @Source table (SourceID int
Using SQL Server 2008 (R2). I have two tables in different databases (with same
I have two tables that I'm joining (in order to get the username from,
I have one table: drupal.comments , with amongst others, the columns: cid: primary key
I have a database, which has 5 tables, but I have problem. In the
I have a table that compares the competitiveness of airline routes in United States.
I have 3 tables on which i need to select data from using ms-access
Suppose you have a trigger on MY_CUSTOMER_TABLE and that it has a variable declared
Edit: OOPS, apparently in my test tables, I forgot to set id as autoincremental.
I think I've stumbled upon a bug in the .NET (or the native windows

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.