Need an example of a query of how to pull record data from three tables?


I need to write a query that pulls information from three different tables, and joins the information from the three tables into one record.

Each table has one field in common with the others.  
That field is RequestID.
From table A I need to pull RequestID, LoadedLOE.
From table B I need to pull RequestID, ReleaseMonth, ReleaseYear.
From table C I need to pull RequestID, Subline.

The query needs to combine these records into my table D.

Thanks in advance for your time.
Who is Participating?
bwasyliukConnect With a Mentor Commented:
Are you inserting this into table D - or do you just need this data as a query record set?

here is the query:

FROM Table1 A
  JOIN Table2 B ON A.RequestID = B.RequestID
  JOIN Table3 C ON A.RequestID = C.RequestID

Does this give you the result set you are looking for?

danrosenthalConnect With a Mentor Commented:
Here is the SELECT:

      a.RequestID, a.LoadedLOE
      ,b.ReleaseMonth, b.ReleaseYear
      tableA a, tableB b, tableC c
WHERE a.RequestID = b.RequestID
AND a.RequestID = c.RequestID

If you want to insert the results into Table D you would do this:

INSERT INTO tableD (requestID, LoadedLOE, ReleaseMonth, etc...)
SELECT a.RequestID
      , a.LoadedLOE
      , b.ReleaseMonth
      , etc....
trailblazzyr55Connect With a Mentor Commented:
here's an easy method to select everything you need into a new table in a single query...

SELECT A.RequestID, B.ReleaseMonth, B.ReleaseYear, C.Subline
INTO Tabel4
FROM Table1 A INNER JOIN Table2 B ON A.RequestID = B.RequestID
              INNER JOIN Table3 C ON A.RequestID = C.RequestID
David WilliamsonIT DirectorCommented:
and yet another variation (just for fun):

select requestid,releasemonth,releaseyear,subline
into table4
  table1 inner join table2 using(requestid)
  inner join table3 using(requestid)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.