SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 joining 3 tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

jonekim
Starting Member

Nepal
35 Posts

Posted - 08/01/2012 :  04:21:34  Show Profile  Reply with Quote
I've 3 tables:
create table t1
(
t1_id int,
t1_name varchar(30)
contraint pk_t1
primary key(t1_id) on [primary]
)

create table t2
(
t2_id int,
t2_name varchar(30)
contraint pk_t2
primary key(t2_id) on [primary]
)

create table t3
(
t1_id int,
t2_id int,
t3_name varchar(30),
t3_price int
foreignkey(t1_id) references t1(t1_id)
foreignkey(t2_id) references t2(t2_id)
)
now by using join, I've to get the name from t1 & t2 via matching ids in t3. I've done it for 2 tables
but can not for 3 tables.
I've tried this but it is not working.

select t1.t1_name,t3.t3_price,t2.t2_name
from t3
inner join t1
on t1.t1_id = t3.t1_id
inner join t2
on t2.t2_id = t3.t3_id
where t3.t3_price = '30'

Edited by - jonekim on 08/01/2012 23:23:08

webfred
Flowing Fount of Yak Knowledge

Germany
8515 Posts

Posted - 08/01/2012 :  04:43:00  Show Profile  Visit webfred's Homepage  Reply with Quote
My doubt is: there is NO relation between table_1 and table_2, hence they can't be joined...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 08/01/2012 :  04:52:35  Show Profile  Reply with Quote
perhaps that should be t2 and not t1 ?

create table t3
(
t1_id int,
t2_id int,
t3_name varchar(30),
t3_price int
foreignkey(t1_id) references t1(t1_id)
foreignkey(t2_id) references t1(t2_id)
)



KH
Time is always against us

Go to Top of Page

jonekim
Starting Member

Nepal
35 Posts

Posted - 08/01/2012 :  08:27:33  Show Profile  Reply with Quote
nop its just typo error. any thank you for the correction.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16746 Posts

Posted - 08/01/2012 :  08:38:14  Show Profile  Reply with Quote
quote:
I've tried this but it is not working.

can you explain what is not working ? Some sample data and expected result will be great


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000