Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2003
    Posts
    223

    Unanswered: How to select record only in one table?

    Hello, everyone:

    I have two tables like:

    Table A
    ID CodeA NumA
    1 D1 289
    2 C4 300
    3 C9 409

    Table B
    ID COdeB NumB
    1 D3 266
    2 C4 300
    3 C7 101

    How to find out the records that is in Table A not in Table B, and in Table B not in Table A? That means C4/300 should not be selected.

    Thanks

    ZYT

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Use the FULL OUTER JOIN:
    Code:
    select	TableA.*,
    	TableB.*
    from	TableA
    	FULL OUTER JOIN TableB
    		on TableA.ID = TableB.ID
    		and TableA.CodeA = TableB.CodeB
    		and TableA.NumA = TableB.NumB
    		
    where	TableA.ID is null
    	or TableB.ID is null
    ...or the UNION method:
    Code:
    select	'TableA' as Source,
    	TableA.*
    from	TableA
    	LEFT OUTER JOIN TableB
    		on TableA.ID = TableB.ID
    		and TableA.CodeA = TableB.CodeB
    		and TableA.NumA = TableB.NumB
    where	TableB.ID is null
    UNION
    select	'TableB' as Source,
    	TableB.*
    from	TableB
    	LEFT OUTER JOIN TableA
    		on TableB.ID = TableA.ID
    		and TableB.CodeB = TableA.CodeA
    		and TableB.NumB = TableA.NumA
    where	TableA.ID is null
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Sep 2004
    Posts
    21

    Another option

    SELECT *
    FROM TABLE_A
    WHERE CODE NOT IN (SELECT CODE FROM TABLE_B)

    This will give you all records in table A that have a code that does not exist in table B.

  4. #4
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    He wants records missing from either table, so he would need a second statement mirroring yours, and then joined to the first with UNION. Similiar to my second example.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •