Query for related tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhcob1
    New Member
    • Feb 2007
    • 19

    Query for related tables

    I have 2 tables, related between the fields 'field1' and 'field2'
    tbl1
    - field1
    - field2
    tbl2
    - field3
    - field4

    I want to run a query so that it displays 4 columns (1 for each field), and displays all the records from tbl1 and if that record is related to a record in tbl2 to display the remaining 2 fields. Else, if there is no related record in tbl2, to leave the last 2 columns blank

    The problem is, the query i get will only display records in tbl1 that have a related record in tbl2.

    EXCUSE THE TERRIBLE EXAMPLE OF THE PROBLEM

    tbl1
    field1 field2
    cat 1
    dog 3
    mouse 4
    horse 5

    tbl2
    field3 field4
    cat meow
    horse nahh

    What i would like to query to output would be
    cat 1 cat meow
    dog 3
    mouse 4
    horse 5 horse nahh

    All i can get is
    cat 1 cat meow
    horse 5 horse nahh

    Any suggestions.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Do a left join. This will include all records in tbl1 regardless of whether or not there's a matching record in table 2.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      In your example it would be :
      Code:
      SELECT tbl1.Field1,
             tbl1.Field2,
             tbl2.Field3,
             tbl2.Field4
      FROM tbl1 LEFT JOIN tbl2
        ON tbl1.Field1=tbl2.Field3

      Comment

      • bhcob1
        New Member
        • Feb 2007
        • 19

        #4
        Thanks guys, LEFT JOIN worked great. Just what I was looking for

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Pleased to hear it - We aim to please :)

          Comment

          Working...