MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 24th, 2004, 10:54 AM
TRoper TRoper is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 4 TRoper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question with 3 table query

I need to return 1 row of data where 3 tables are linked.
Table 1 (comp) is linked to Table 2 (cat) on Company ID
Table 2 is linked to Table 3 (ref_cat) on CatID

Table 1 contains the Company Information ie CompanyID

Table 2 can have 1-3 listings for one CompanyID each with a unique CatID's (Industry, Category, Sub Category levels)

Table 3 contains the unique CatID, a Parent ID, Name.
The records that have a Parent ID of 0 are the Industry.
The Category record is linked when the Parent ID is the same as the CatID of the top level.
The Sub Category is linked when the Parent ID is the same as the CatID of the Category level.

The Sql I have so far that does not return all the data on one row is below the table data.

Here is what the table data looks like.

Table 1 company data
CompanyId - CompanyName
12345 - ABCDE
67890 - VWXYZ

Table 2 category data
CompanyId CategoryId
12345 - 1
12345 - 1002
12345 - 2330
67890 - 7
67890 - 1063

Table 3 ref_category data
CategoryId - ParentId - Name
1 - 0 - Automotive
1002 - 1 - Auto Repair and Service
2001 - 1002 - All Repairs and Service
7 - 0 - Home Services
1063 - 7 - Landscape, Decks, Fencing


My Code:
SELECT comp.Name, cat.companyID, cat.CategoryID, ref_cat.categoryID, ref_cat.parentID, ref_cat.name
FROM company as comp
join category as cat on cat.companyid = comp.companyid
join ref_category as ref_cat on ref_cat.categoryId = cat.categoryID
Where comp.companyID in (2013, 2947)
Order by comp.CompanyId

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Question with 3 table query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway