table1
(
id int identity(1,1),
name varchar(80),
userid int
)
table2
(
id int,
fuserid int ,
goodid int )
table3
(
id int indentity(1,1),
name varchar(80),
img varchar(100)
)
table1.userid=table2.fuserid
table2.goodid= table3.id 问题是如何通过这种关系获得table3里的name img??
(
id int identity(1,1),
name varchar(80),
userid int
)
table2
(
id int,
fuserid int ,
goodid int )
table3
(
id int indentity(1,1),
name varchar(80),
img varchar(100)
)
table1.userid=table2.fuserid
table2.goodid= table3.id 问题是如何通过这种关系获得table3里的name img??
from table1,table2,table3
where table1.userid=table2.fuserid and table2.goodid= table3.id
select c.name,c.img
from table1 a join table2 b on a.userid = b.fuserid
join table3 c on b.goodid = c.id
from table1 a,table2 b,table3 c
where a.userid=b.fuserid and b.goodid= c.id
from table1 inner join table2 on table1.userid=table2.fuserid inner jointable3 on table2.goodid= table3.id
如果有四个表呢,比如
table4
(
id int identity(1,1),
tid int ,---对应表三的自动编号
endstr varchar(50)
)根据第一个表,获取endstr
select c.name,c.img
from table1 a join table2 b on a.userid = b.fuserid
join table3 c on b.goodid = c.id
join table3 d on d.tid=c.id
select c.name,c.img,d.endstr
from table1 a join table2 b on a.userid = b.fuserid
join table3 c on b.goodid = c.id
join table3 d on d.tid=c.id
from
table1 a , table2 b ,table3 c,table4 d
where
a.userid = b.fuserid
and
b.goodid = c.id
and
d.tid=c.id
有几个表就按条件连接几个表,同时注意下 inner join left join right join 的逻辑。