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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 joining 3 tables

Author  Topic 

jonekim
Starting Member

35 Posts

Posted - 2012-08-01 : 04:21:34
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'

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-08-01 : 04:43:00
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)

17689 Posts

Posted - 2012-08-01 : 04:52:35
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

jonekim
Starting Member

35 Posts

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

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-01 : 08:38:14
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
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -