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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old June 25th, 2004, 08:36 AM
eoin_ie eoin_ie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 62 eoin_ie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 44 m 50 sec
Reputation Power: 5
Multiple Table Select

Hey everyone, I have a question regarding an SQL query. I'm working on a Web App in .NET at the moment and part of the project is to produce a report of all the information about the clients. Now, there are 4 tables in question: Client, Details, Appointments and RefItem. Now, a lot of the information stored in the first 3 tables are IDs and the names corresponding to those IDs are in the RefItem table.

The RefItem table has the columns ItemID, GroupID, Name, HelpText and Active. For example, a row in the Appoinment table might contain the ID 150 under the AppointmentStatusID. In RefItem, the ID 150 corresponds with "Scheduled". So I have

Code:
SELECT Name FROM RefItem WHERE RefItem.ItemID = Appointment.AppointmentStatusID


This works fine for selecting one name, but my report requires pretty much every name. I was told I'd had to use INNER JOIN or LEFT OUTER JOIN but I can't seem to figure it out. If anyone has any info, please let me know!! Thanks

Reply With Quote
  #2  
Old June 28th, 2004, 01:44 AM
Lethean Lethean is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Gothenburg Sweden
Posts: 28 Lethean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I would simply use the following code for such joining. (I never liked the outer inner left right joining it might be better but it doesn't make sense when debugging sql syntax as it doesn't say much about the join. So I would do the following:
Code:
 SELECT [colnames] FROM [table1], [table2], ....
WHERE table1.field = table2.id AND
table1.field = table3.id .... 


this way one can easily see what columns that are joined and one doesn't need a whole lot of complex JOIN commands. (OK it might be extra code and the Carthesian Product might be more time consuming but it works.)

So if i get it right your code would be something like this
Code:
 SELECT Name 
FROM RefItem, Client, Details, Appointments 
WHERE RefItem.ItemID = Appointment.AppointmentStatusID,
RefItem.GroupID = Client.id? ...


Is this what you wanted?

Reply With Quote
  #3  
Old June 28th, 2004, 03:14 AM
eoin_ie eoin_ie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 62 eoin_ie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 44 m 50 sec
Reputation Power: 5
Yes and No. The problem is, and I've had this problem before, the RefItem.ItemID is a whole range of different values and if I do the query like you have above, it will only match with the first regular join I do. The rest won't return anything. E.g. : Your code above will return the name of the appointment status, but won't return anything else because it will still keep looking for the same ID that the appointmentstatusID is. It's kinda difficult to explain, needless to say that I've tried out the method suggested by you above and it didn't work. It appears that INNER JOIN is the only way to go...unfortunately.

Reply With Quote
  #4  
Old June 28th, 2004, 03:23 AM
Lethean Lethean is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: Gothenburg Sweden
Posts: 28 Lethean User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I dont understand what it is you want to join but if you specify it further like if you tell em what fields the table have and what fields should be joined i might be able to help.

Do you want to join the RefItem.ItemID to several different tables? ie something like this?:
Code:
SELECT Name FROM RefItem, Client
where RefItem.ItemID = Client.ID
UNION
SELECT Name FROM RefItem, Appointment
where RefItem.ItemID = AppointmentStatusID


Or am I wrong please try to explain further.

Reply With Quote
  #5  
Old June 29th, 2004, 07:09 AM
eoin_ie eoin_ie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 62 eoin_ie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 44 m 50 sec
Reputation Power: 5
I managed to get it working, here is a brief description of what I did:

Code:
SELECT Client.ClientID, Ref1.Name as ClientType, Ref2.Name as ReferredBy

FROM Client
LEFT OUTER JOIN ReferenceItem Ref1 ON Client.ClientTypeID = Ref1.ItemID,
Appointment
LEFT OUTER JOIN ReferenceItem Ref2 ON Appointment.ReferredByID = Ref2.ItemID
WHERE Client.ClientID = Appointment.ClientID


There was a lot more items to be selected but I kept it short for clarity. I performed multiple joins on the ReferenceItem table as I was getting the name for different IDs all the time. If I had one join, I would only have been able to select one name, based on one ID. It's a little complicated...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Multiple Table Select


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 5 hosted by Hostway