• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

SQL Query Help

I need some help with a query I am trying to formulate - here is the scenario - I have 2 tables Table A has a FK to Table B - I need data from Table A provided rows for that data exist in Table B so this query does the job

SELECT TableA.FK1 FROM TableA INNER JOIN TableB ON TableA.FK1 = TableB.FK1

Now once I have the common FK I need to pull some data from table B - TableB has FK's to 2 other tables - how do I write one query to get all the data I need i.e that from table A and table B ?

Thanks
0
abuyusuf35
Asked:
abuyusuf35
1 Solution
 
Forefront_Data_SolutionsCommented:
Try:

SELECT TableC.Field1, TableD.Field1
FROM TableA
INNER JOIN TableB ON TableA.FK1 = TableB.FK1
INNER JOIN TableC ON TableB.FK2 = TableC.FK2
INNER JOIN TableD on TableB.FK3 = TableD.FK3
0
 
yesthatbobCommented:
You can nest joins. Let's assume your other tables are TableC and TableD:

SELECT TableA.FK1, TableB.FK_C, TableB.FK_D
FROM TableA INNER JOIN TableB ON TableA.FK1 = TableB.FK1
    INNER JOIN TableC ON TableB.FK_C = TableC.FK_C
    INNER JOIN TableD ON TableB.FK_D = TableD.FK_D

Open in new window

0
 
Matthew KellyCommented:
You can just add the TableB columns as a comma separated list between the SELECT and FROM

SELECT
TableA.FK1,
TableB.FK1
FROM TableA
INNER JOIN TableB ON TableA.FK1 = TableB.FK1

More examples: http://blogs.msdn.com/b/alikl/archive/2010/07/20/sql-server-select-query-amp-join-types.aspx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now