Solved

SQL Query Help

Posted on 2011-09-08
3
167 Views
Last Modified: 2012-05-12
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
Comment
Question by:abuyusuf35
3 Comments
 
LVL 8

Expert Comment

by:Forefront_Data_Solutions
ID: 36507230
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
 
LVL 4

Expert Comment

by:yesthatbob
ID: 36507237
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
 
LVL 18

Accepted Solution

by:
Matthew Kelly earned 500 total points
ID: 36507238
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now