Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Joining 3 tables

Status
Not open for further replies.

mkp2004

Programmer
May 27, 2004
11
US
Here I have 3 tables and I need to join them with the following criteria.Please help me out..

id emp_name emp_address
1 Manu sdfsdfdsfs Table-1
2 Anu sdfsdfdsfsdf
3 Vinu sdfsdfsdfsdf

id age sex
2 12 m
3 34 f Table 2


id course dept
2 x wer
3 y rty
1 z rtyt Table 3


Here I have 3 tables..I want to have a query where all the details of table1 should be there and from table 1 and table 2 i just want the records which are matching with the corressponding id in the table1


 
Code:
Select * from Table1 left join Table2 on Table1.ID=Table2.ID
-Karl

[red] Cursors, Triggers and User Definded Functions are part of the Axis of Evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
There is no join shown for the third table.I think I didn't put it properly..I need to get all the data from table1 and the matching dataset from table 2 and table3 (where the id's should match)..Sorry for not putting it properly
 
Code:
Select * from Table1 left join Table2 on Table1.ID=Table2.ID
left join Table3 on Table1.ID=Table3.ID
-Karl


[red] Cursors, Triggers and User Definded Functions are part of the Axis of Evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
 
This example involves three tables: company, asset, and employee. Every record from company is included. If a matching record in asset or employee exists, it is included. I took a couple of fields from each of the three tables. The match is based on the the companyId field...

SELECT
dbo.company.companyID,
dbo.company.name,
dbo.asset.value,
dbo.asset.acquisitionDate,
dbo.employee.lastName,
dbo.employee.firstName
FROM dbo.company
LEFT OUTER JOIN dbo.asset ON dbo.company.companyID = dbo.asset.companyID
LEFT OUTER JOIN dbo.employee ON dbo.company.companyID = dbo.employee.companyID
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top