SQL Server - joining two tables
Asked By shekhar kumar on 18-Jun-09 04:56 AM
I have two table eg. fundtransfer and bankmaster. I have key of bankmaster twice in fundtransfer eg. tobank and frombank.
How can I write query to get both the names. If I use inner join then it giving only one name.
Thanks in advance.
Vasanthakumar D replied to shekhar kumar on 18-Jun-09 05:23 AM
Hi,
try this...
select
a.bankName, b.bankName from fundtransfer c
inner
join bankmaster a on c.fromBank = a.bankId
inner
join bankmaster b on c.toBank = b.bankId
thanks
shekhar kumar replied to Vasanthakumar D on 18-Jun-09 05:34 AM
thanks but it is giving duplicate values should I use distinct?
thanks
yeh you should use distinct
san thanki replied to shekhar kumar on 18-Jun-09 05:40 AM
use distinct keyword with query.
welcome... and yes
Vasanthakumar D replied to shekhar kumar on 18-Jun-09 05:43 AM
still getting more than one record
shekhar kumar replied to san thanki on 18-Jun-09 05:45 AM
It should fetch only one record but it is woring as cross job fundtransfer has only one record an it is giving mor ethan one record.
Thanks again
not working
shekhar kumar replied to shekhar kumar on 18-Jun-09 06:03 AM
Getting more than one record corropoding to one record woring as cross join.
still getting more than one record corrosponding to one record
shekhar kumar replied to Vasanthakumar D on 18-Jun-09 06:04 AM
I am getting more than one record corrosponding to one record in the fundtransfer.
Plz help me
thanks
RE
Ravenet Rasaiyah replied to shekhar kumar on 18-Jun-09 06:09 AM
Hi
try this
select t1.bankName, t2.bankName from fundtransfer t3
LEFT OUTERjoin bankmaster t1 on t3.fromBank = t1.bankId
inner join bankmaster t2 on t3.toBank = t2.bankId
Thank you
http://www.codegain.com
thanks it's wroking fine
shekhar kumar replied to Ravenet Rasaiyah on 18-Jun-09 06:24 AM
Thanks a lot. Now its working properly. :)
Thanks
cool
Ravenet Rasaiyah replied to shekhar kumar on 18-Jun-09 06:25 AM
Vasanthakumar D replied to shekhar kumar on 18-Jun-09 06:26 AM
Hi,
you need to include some more unique columns in 'join condiontions'....