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 November 30th, 2004, 10:19 AM
bdee1 bdee1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 5 bdee1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
how do i write this query

ok i have table1, table 2 and table 3.

these table have some common feild names. table1,2 and 3 all have a,b,c and d as field names. each table has other field names too but the ones they all have in common are a,b,c and d.

so i would like to write a query that returns all rows from all 3 tables where column d is greater than 5 and less than 10.

so basically i want it to treat the records from all 3 tables ad one big dataset.

how would i write a query to do this.

i know i could say:
SELECT a,b,c,d
FROM table1,table2,table3

but what gets me is the WHERE clause

do i have to say WHERE table1.d >5 AND table1.d <10 OR table2.d>5 AND table2.d <10 OR table3.d>5 AND table3.d <10

??

any guidance please?

Reply With Quote
  #2  
Old November 30th, 2004, 10:33 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,773 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 14 h 9 m 36 sec
Reputation Power: 37
Code:
select 't1',a,b,c,d from tabel1
where d between 6 and 9
union all
select 't2',a,b,c,d from tabel2
where d between 6 and 9
union all
select 't3',a,b,c,d from tabel3
where d between 6 and 9

Reply With Quote
  #3  
Old November 30th, 2004, 10:41 AM
bdee1 bdee1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 5 bdee1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by swampBoogie
Code:
select 't1',a,b,c,d from tabel1
where d between 6 and 9
union all
select 't2',a,b,c,d from tabel2
where d between 6 and 9
union all
select 't3',a,b,c,d from tabel3
where d between 6 and 9



ahh yes! i forgot about union.

2 questions though:

1) what is with the 't1' after the select?
2) what is union all vs. union?

thanks for the quick reply!

Reply With Quote
  #4  
Old November 30th, 2004, 11:01 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,773 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 14 h 9 m 36 sec
Reputation Power: 37
I added the constants 't1' etc so it will be possible to distinguish between records from the different tables.

Union means that all duplicates are removed from the result set whereas union all will retain all duplicates.

(In this case the result would be the same whether using union all or union but the reason for using union all is that the DBMS can skip the elimination of duplicate which results in a performance gain. If there is rather few records in the result it will not be noticeable.)

Reply With Quote
  #5  
Old November 30th, 2004, 11:05 AM
bdee1 bdee1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 5 bdee1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by swampBoogie
I added the constants 't1' etc so it will be possible to distinguish between records from the different tables.

Union means that all duplicates are removed from the result set whereas union all will retain all duplicates.

(In this case the result would be the same whether using union all or union but the reason for using union all is that the DBMS can skip the elimination of duplicate which results in a performance gain. If there is rather few records in the result it will not be noticeable.)

gotcha - thanks for the great info!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > how do i write this 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 6 hosted by Hostway