SharkBait Posted November 1, 2006 Share Posted November 1, 2006 I have two tables that share a similar field. Lets call it SN.Table1.SNTable2.SNNow 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] Quote Link to comment Share on other sites More sharing options...
alpine Posted November 1, 2006 Share Posted November 1, 2006 [code]SELECT T1.board_sn, T2.serial_number FROM T1, T2 WHERE T1.board_sn <> T2.serial_number[/code] Quote Link to comment Share on other sites More sharing options...
SharkBait Posted November 1, 2006 Author Share Posted November 1, 2006 I've done this:[code]SELECT T1.board_sn, T2.serial_numberFROM feedback.RMAs AS T1, smt.entry AS T2WHERE 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_numberFROM feedback.RMAs AS T1, smt.entry AS T2WHERE T1.board_sn = T2.serial_number[/code]It will return only 453 rows. I am unsure what I am doing wrong :) Quote Link to comment Share on other sites More sharing options...
shoz Posted November 1, 2006 Share Posted November 1, 2006 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]SELECTT1.board_snFROMfeedback.RMAs AS T1LEFT JOINsmt.entry AS T2ONT1.board_sn = T2.serial_numberWHERET2.serial_number IS NULL[/code]Note the use of the LEFT JOIN.http://dev.mysql.com/doc/refman/4.1/en/join.html Quote Link to comment Share on other sites More sharing options...
fenway Posted November 1, 2006 Share Posted November 1, 2006 Using the LEFT JOIN...IS NULL method is the preferred way for a variety of reasons, not the of which is that you'll never return too many rows by missing the on clause (i.e. by using <>). Quote Link to comment Share on other sites More sharing options...
SharkBait Posted November 1, 2006 Author Share Posted November 1, 2006 I need to leanr more about JOINs so far their great :) So LEFT JOIN will include the information from the second table onto the frist table?What is the difference between an INNER JOIN and a LEFT JOIN? Quote Link to comment Share on other sites More sharing options...
fenway Posted November 1, 2006 Share Posted November 1, 2006 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.