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 October 28th, 2004, 04:50 AM
spacdude spacdude is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Hampshire, England
Posts: 37 spacdude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 25 sec
Reputation Power: 5
Unhappy 5 Table select query

Hello,

I'm having trouble writing a select query with 5 tables becuase I don't want a huge amount of joins making a large overhead on the server.
The first 3 tables are all linked by one key and the other two tables are lookup tables for two codes that appear in the first table.

I want to select 5 columns from table 1, 3 columns from table 2 and 1 column from table 3. They are all linked by a key.

Is some kind of union involved, I couldn't get one working becuase of the differing number of columns I want from the tables.

Table 1
Table 2 All 3 linked by a key
Table 3


Table 4 Both of these relate to codes in table1
Table 5


Thanks in advance if anyone can give me any clues as I'm rather confused.


Reply With Quote
  #2  
Old October 28th, 2004, 05:13 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,962 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 29 m 38 sec
Reputation Power: 1024
Quote:
I want to select 5 columns from table 1, 3 columns from table 2 and 1 column from table 3. They are all linked by a key.

Code:
select t1.col1
     , t1.col2
     , t1.col3
     , t1.col4
     , t1.col5
     , t4.lookup1
     , t5.lookup2
     , t2.cola
     , t2.colb
     , t2.colc
     , t3.colx
  from table1 as t1
inner
  join table2 as t2
    on t1.key = t2.key  
inner
  join table3 as t3
    on t1.key = t3.key  
inner
  join table4 as t4
    on t1.colfoo = t4.key  
inner
  join table5 as t5
    on t1.colbar = t5.key  
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #3  
Old October 28th, 2004, 05:45 AM
spacdude spacdude is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Hampshire, England
Posts: 37 spacdude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 25 sec
Reputation Power: 5
Thanks very much for that method r937, the first 3 tables im using have huge amounts of data in so im wondering if there is a way to do this with a smaller number of joins to try and cut down the processing on the server. maybe take the info from the the first table and the lookups in one query and then the other two tables in another query? i'm trying to cut down on processing overheads.

Reply With Quote
  #4  
Old October 28th, 2004, 05:53 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,962 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 29 m 38 sec
Reputation Power: 1024
have you declared the proper indexes?

Reply With Quote
  #5  
Old October 28th, 2004, 06:04 AM
spacdude spacdude is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Hampshire, England
Posts: 37 spacdude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 25 sec
Reputation Power: 5
I've looked in the sysindexes table and their appears to be indexes for each of the table i need to use though i have no experience of indexes.

Reply With Quote
  #6  
Old October 28th, 2004, 06:06 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,962 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 29 m 38 sec
Reputation Power: 1024
there is really no other way to obtain the data you want besides joining the tables

you should take the time to learn indexing and how to interpret the output of EXPLAIN statements

in the meantime, don't worry about the overhead

Reply With Quote
  #7  
Old October 28th, 2004, 06:20 AM
spacdude spacdude is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Hampshire, England
Posts: 37 spacdude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 30 m 25 sec
Reputation Power: 5
ok thanks for ur helpful advice as always

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > 5 Table select 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 5 hosted by Hostway
Stay green...Green IT