Unix Technical Forum

Trying to join three tables

This is a discussion on Trying to join three tables within the SQL Server forums, part of the Microsoft SQL Server category; --> Having problems joining three tables. I only want the Title field but need them to give me the correct ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:40 PM
JJ297
 
Posts: n/a
Default Trying to join three tables

Having problems joining three tables. I only want the Title field but
need them to give me the correct data.

Here are my three tables and their fields.

1. Titles
TitleID
Title
[description]


2. Classifications
ClassificationID
[Description]

3. Titleclassification
ClassificationID
TitleID

This is my stored procedure thus far but getting incorrect syntax
error.

select Titles.Title

>From Titles


Inner Join Titleclassification on classifications.classificationid =
titleclassification.classificationid
Join titlecassification.titleid = titles.titleid

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:40 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Trying to join three tables

JJ297 (nc297@yahoo.com) writes:
> Having problems joining three tables. I only want the Title field but
> need them to give me the correct data.
>
> Here are my three tables and their fields.
>
> 1. Titles
> TitleID
> Title
> [description]
>
>
> 2. Classifications
> ClassificationID
> [Description]
>
> 3. Titleclassification
> ClassificationID
> TitleID
>
> This is my stored procedure thus far but getting incorrect syntax
> error.
>
> select Titles.Title
> From Titles
> Inner Join Titleclassification on classifications.classificationid =
> titleclassification.classificationid
> Join titlecassification.titleid = titles.titleid


The form for a three-way join would be:

SELECT ...
FROM tbl1 t1
JOIN tbl2 t2 ON t1.somecol = t2.somecol
JOIN tbl3 t3 ON t2.someothercol = t3.someothercol

This pattern should give you some idea to write the query. Notice that I
use aliases. This helps to make the query less verbose. If you use the
table names as prefixes, you can easily lose the sight of the forest
for all the trees.

However, I think your correct query needs to use exists instead:

SELECT T.TitleID
FROM Titles T
WHERE EXISTS (SELECT *
FROM Titleclassifications TC
JOIN Classifications C
ON TC.ClassificationID = C.ClassificationID
WHERE TC.TitleID = T.TitleID)


Else you will get a lot of duplicates.



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:11 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com