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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:28:50+00:00 2026-06-02T17:28:50+00:00

I’m trying to compare 2 mySQL tables to find differences between them. A record

  • 0

I’m trying to compare 2 mySQL tables to find differences between them. A record may be found in TableA but not in TableB, or vice versa.

My tables are as follows:

TableA

Name    A1  A2  B1  B2
------------------------
John    11  12  21  23
John    11  12  21  22
John    33  34  31  33
Mary    41  42  54  55
Mary    71  72  81  82
Mary    41  42  51  52

TableB

Name    A1  A2  B1  B2  C   D
---------------------------------
John    11  12  21  22  999 999
John    21  23  11  12  999 999
John    31  32  33  34  999 999
Mary    41  42  51  52  999 999
Mary    54  55  41  42  999 999

Columns A1 and A2 is considered a group, and B1 and B2 considered another group. For a record to be considered found in both tables, I need

- TableA(A1,A2) = TableB(A1,A2) AND TableA(B1,B2) = TableB(B1,B2)

OR

- TableA(A1,A2) = TableB(B1,B2) AND TableA(B1,B2) = TableB(A1,A2)

For the 2 tables, above, I would compare all of TableA’s John to all of TableB’s John, and all of TableA’s Mary to all of TableB’s Mary.

I should get the output

Name    A1  A2  B1  B2  C   D
-----------------------------------------------
John    31  32  33  34  999 999 (from TableB)
Mary    41  42  54  55          (from TableA)
Mary    71  72  81  82          (from TableA)
Mary    54  55  41  42  999 999 (from TableB)

I’m new to mySQL, and the above seems so complicated to me that I’m not even sure where to start.

I would really appreciate any help on this.

  • 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-02T17:28:51+00:00Added an answer on June 2, 2026 at 5:28 pm

    If I understood you correctly, you need to issue two queries: one for finding records from TableA not existing in TableB, and second one for the opposite situation. Note that in one case it’s LEFT JOIN and in the second case it’s RIGHT JOIN.

    SELECT a.*, '' AS C, '' AS D, '(from TableA)' AS 'table'
    FROM TableA AS a
    LEFT JOIN TableB AS b
      ON ((a.A1 = b.A1 AND a.A2 = b.A2 AND a.B1 = b.B1 AND a.B2 = b.B2)
          OR (a.A1 = b.B1 AND a.A2 = b.B2 AND a.B1 = b.A1 AND a.B2 = b.A2))
        AND a.Name = b.Name
    WHERE b.Name IS NULL
    
    UNION
    
    SELECT b.*, '(from TableB)' AS 'table'
    FROM TableA AS a
    RIGHT JOIN TableB AS b
      ON ((a.A1 = b.A1 AND a.A2 = b.A2 AND a.B1 = b.B1 AND a.B2 = b.B2)
          OR (a.A1 = b.B1 AND a.A2 = b.B2 AND a.B1 = b.A1 AND a.B2 = b.A2))
        AND a.Name = b.Name
    WHERE a.Name IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.