Results 1 to 13 of 13
  1. #1
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169

    Query on related tables question

    Is it possible to run a query on two related tables to return records where the value of a field in the Main table is not null and a value of a different field in the Related table is not null. So by example if I had Table3 and Table4 related by NameID. My desire is to return all records that have a value in the LastName field in Table3 and a value in the City field in Table4. Is this possible?


    I have a Left Join query that is returning more records than I want to see. In the attached jpeg (JoinQry.jpg) I am desiring only the records that have a red arrow. Any help is appreciated. Thanks.
    Code:
    SELECT Table3.NameID, Table3.FirstName, Table3.LastName, Table4.City
    FROM Table3 LEFT JOIN Table4 ON Table3.NameID=Table4.NameID;

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    SELECT Table3.NameID, Table3.FirstName, Table3.LastName, Table4.City FROM Table3 LEFT JOIN Table4 ON Table3.NameID=Table4.NameID

    WHERE
    Table3.LastName Is Not Null AND Table4.City Is Not Null
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169

    Updated table4 and code

    Sorry, I attached the wrong screen shot of table4 and the join query results. These are correct.
    Last edited by jpkeller55; 09-28-2010 at 08:46 PM.

  4. #4
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Paul, that is producing the following result:

  5. #5
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    What shows in the white section is what I would like to include in the query results while excluding what is in green.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    It sounds like you want to reverse the test:

    WHERE Table3.LastName Is Null AND Table4.City Is Null
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by pbaldy View Post
    Try

    SELECT Table3.NameID, Table3.FirstName, Table3.LastName, Table4.City FROM Table3 LEFT JOIN Table4 ON Table3.NameID=Table4.NameID

    WHERE
    Table3.LastName Is Not Null AND Table4.City Is Not Null
    Paul,

    I believe this query is running fine, but the LEFT JOIN is causing the problem. I think what it happening is that the nature of the join is trumping everything else, which it has a RIGHT too anyway (no pun intended).

    <edit>
    I'm not making much sense above...

    Jim,

    I think what you might be looking for is this:
    Code:
    WHERE 
    
    (Table3.LastName Is Not Null AND Table4.City Is Not Null) OR
    
    (Table3.LastName Is Null AND Table4.City Is Not Null) OR
    
    (Table3.LastName Is Not Null AND Table4.City Is Null)
    Aren't you looking for any combination other than two nulls?

  8. #8
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Hey Adam, that is what I am lookinmg for. I will give it a shot

  9. #9
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Yep, that did the trick...thanks Adam. Attached are the results of the query and the code.
    Code:
    SELECT Table3.NameID, Table3.FirstName, Table3.LastName, Table4.City, Table4.Car
    FROM Table3 INNER JOIN Table4 ON Table3.NameID = Table4.NameID
    WHERE 
    Table3.LastName Is Not Null AND Table4.City Is Not Null OR
    Table3.LastName Is Null AND Table4.City Is Not Null OR
    Table3.LastName Is Not Null AND Table4.City Is Null
    ORDER BY Table3.FirstName;

  10. #10
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    good deal Jim. I went ahead and marked this thread as solved...hope that is OK!

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Looks like you were right Adam; good job. I'm not that good doing SQL in my head.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    jpkeller55 is offline Competent Performer
    Windows XP Access 2003
    Join Date
    Sep 2010
    Posts
    169
    Yes, thanks for marking it solved Adam!

  13. #13
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by pbaldy View Post
    Looks like you were right Adam; good job. I'm not that good doing SQL in my head.
    I would think that SQL Server would have burned sql statements of any kind into your head by this point...No?

    And the credit is shared...I don't do 'exclusive'.

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

Similar Threads

  1. Query with related tables question
    By jpkeller55 in forum Access
    Replies: 4
    Last Post: 09-25-2010, 04:29 AM
  2. Not able to make Form from related Tables
    By a1y1a1y1 in forum Forms
    Replies: 5
    Last Post: 01-02-2010, 12:33 PM
  3. Table related to multiple tables by single ID
    By MrTumnus in forum Access
    Replies: 1
    Last Post: 11-17-2009, 02:05 PM
  4. 3 Related Dependent Tables
    By jbarrum in forum Access
    Replies: 0
    Last Post: 11-17-2009, 11:27 AM
  5. Split a table into related tables
    By triley01 in forum Database Design
    Replies: 1
    Last Post: 03-12-2009, 02:38 PM

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