Jump to content

Matching across two tables


SharkBait

Recommended Posts

I have two tables that share a similar field.  Lets call it SN.

Table1.SN

Table2.SN

Now I want to return all the results where Table2.SN <> Table1.SN so that I can see which entries Table1.SN has but Table2.SN does not.

How do I go about doing this?

This is what I had, but its obviously wrong because it return 42million rows ;)

[code]
SELECT T1.board_sn, T2.serial_number FROM feedback.RMAs AS T1 OUTER JOIN smt.entry AS T2 ON (T1.board_sn <> T2.serial_number)
[/code]
Link to comment
Share on other sites

I've done this:
[code]

SELECT T1.board_sn, T2.serial_number
FROM feedback.RMAs AS T1, smt.entry AS T2
WHERE T1.board_sn <> T2.serial_number

[/code]

it returns 43million+ rows.  1 table has 2800rows the other table has like 15,000 rows, so I am not sure what is being duplicated or why.

When I do it like this:
[code]

SELECT T1.board_sn, T2.serial_number
FROM feedback.RMAs AS T1, smt.entry AS T2
WHERE T1.board_sn = T2.serial_number

[/code]

It will return only 453 rows. I am unsure what I am doing wrong :)
Link to comment
Share on other sites

The following should show the board_sn's in T1 that have no reference in T2. To have the query show the opposite, change the order of the tables in the FROM clause and change the WHERE clause condition to T1.board_sn IS NULL.

[code]
SELECT
T1.board_sn
FROM
feedback.RMAs AS T1
LEFT JOIN
smt.entry AS T2
ON
T1.board_sn = T2.serial_number
WHERE
T2.serial_number IS NULL
[/code]

Note the use of the LEFT JOIN.

http://dev.mysql.com/doc/refman/4.1/en/join.html
Link to comment
Share on other sites

JOINs will simply "combine" the matching records from two tables based on your ON clause -- how these are combined depends on the type of JOIN.  INNER JOINs will only return records if matching records are found in both tables; LEFT JOINs will return the record from the table on the "left" of the JOIN, and the record on the "right" if it matches, otherwise, NULLs for each column in the right table.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.