Results 1 to 5 of 5
  1. #1
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34

    Checking if column combinations exist in another table

    For simplicity I will use generic names



    Table1 and Table2 both have an ID and Description columns. However, Table2 calls its ID column XY

    I want to see if the combination of ID and Description from Table1 does not exist in Table2

    I know I can do it with one column by saying SELECT [ID] FROM [Table1] WHERE [ID] NOT IN (SELECT DISTINCT [XY] FROM [Table2])

    How can I write the query where it's something like this:

    SELECT [ID], [Description] FROM [Table1] WHERE [ID] NOT IN (SELECT DISTINCT [XY], [Description] FROM [Table2])

    Will it know to match up ID with XY, and look to compare the pair ID, Description with XY, Description?

    I tried that and it gave some error about using EXISTS, so I tried to say "NOT EXISTS" and I get a syntax error

  2. #2
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34
    Something screwy is going on, because when I do the simple query of NOT IN on [Description] no records are returned, even though I can see with my own eyes that there are Descriptions that do not exist.

    Can Access not perform a query like that on a Text field or something?

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    An unmatched query should do it (joining on two fields)

    Try this:
    Code:
    SELECT Table1.ID, Table1.Description
    FROM Table1
    LEFT JOIN Table2
    ON Table1.ID = Table2.XY
    AND Table1.Description = Table2.Description
    WHERE Table2.XY Is Null;

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    To do kind of like the method you were trying, you could also use this code:
    Code:
    SELECT Table1.ID, Table1.Description
    FROM Table1
    WHERE (Table1.ID & Table1.Description)  Not In
    (SELECT (Table2.XY & Table2.Description)
    FROM Table2);

  5. #5
    blacksaibot is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Jan 2010
    Posts
    34
    Quote Originally Posted by JoeM View Post
    An unmatched query should do it (joining on two fields)

    Try this:
    Code:
    SELECT Table1.ID, Table1.Description
    FROM Table1
    LEFT JOIN Table2
    ON Table1.ID = Table2.XY
    AND Table1.Description = Table2.Description
    WHERE Table2.XY Is Null;
    Thanks! Just tried it and it works.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. 'Mapping Table' query including all combinations
    By rewritable in forum Queries
    Replies: 2
    Last Post: 06-09-2012, 04:34 PM
  2. Replies: 3
    Last Post: 03-14-2012, 06:48 AM
  3. Checking if data already exist in a Table
    By Phillsoft in forum Forms
    Replies: 2
    Last Post: 08-04-2011, 02:07 PM
  4. Checking if data already exist in a Table
    By Phillsoft in forum Forms
    Replies: 1
    Last Post: 08-04-2011, 08:03 AM
  5. Replies: 5
    Last Post: 05-26-2010, 07:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums