Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Nitu
Yak Posting Veteran
81 Posts |
Posted - 2006-03-20 : 17:21:39
|
| Hi,I need to write a query where i have four different tables - (tables 1 to 4) to join to another table - (table5). Based on the value of a particular field (lets say field4) in table5 the corresponding records are in those different four tables.The relation b/n the five tables is like this,Table5-field1 (pk) field2 field3 field4Table1 has one to many records with table5, though not all records but only those who have field4 = 1Table1-field1 field21 field31The same goes with the rest of the tables.So can i write a query like this,select * from table5inner join on table1 when table5.field4 = 1 or inner join on table2 when table5.field4 = 2 or inner join on table3 when table5.field4 = 3or inner join on table4 when table5.field4 = 4So any suggestions on how to do the above.Thanks a lot in advance,Nitu |
|
|
Nitu
Yak Posting Veteran
81 Posts |
Posted - 2006-03-20 : 17:48:48
|
quote: Originally posted by Nitu Hi,I need to write a query where i have four different tables - (tables 1 to 4) to join to another table - (table5). Based on the value of a particular field (lets say field4) in table5 the corresponding records are in those different four tables.The relation b/n the five tables is like this,Table5-field1 (pk) field2 field3 field4Table1 has one to many records with table5, though not all records but only those who have field4 = 1Table1-field1 field21 field31The same goes with the rest of the tables.So can i write a query like this,select * from table5inner join on table1 when table5.field4 = 1 or inner join on table2 when table5.field4 = 2 or inner join on table3 when table5.field4 = 3or inner join on table4 when table5.field4 = 4So any suggestions on how to do the above.Thanks a lot in advance,Nitu
I just thought of a solution. I am going to write four different queries and then do a Union of all.If somebody can think of a better way, please reply.thanks a lot in advance.Nitu |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-03-20 : 18:53:52
|
| The best way to do this is to redesign your database. You should not have tables designed this way.Tara Kizeraka tduggan |
 |
|
|
Nitu
Yak Posting Veteran
81 Posts |
Posted - 2006-03-21 : 10:50:28
|
I know, but hey i just work here, and i joined in the middle, when they have been using this for more than 2 years now. And my lead starts hating me the minute i talk about the design of the database. So what can i do, i believe thats part of the job. Anyway, do u think there is any other way around for doing what i posted.Thanks in advance,Nitu |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-21 : 11:45:31
|
| Given your (unfortunate) situation, your suggested solution seems like the best method to me.Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
|
|
|
|
|