|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
Quote:
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! |
|
#4
|
|||
|
|||
|
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.) |
|
#5
|
|||
|
|||
|
Quote:
gotcha - thanks for the great info!! |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > how do i write this query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|