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??

解决方案 »

  1.   

    select table3.name,table3.img
     from table1,table2,table3
    where table1.userid=table2.fuserid and table2.goodid= table3.id  
      

  2.   


    select c.name,c.img
    from table1 a join table2 b on a.userid = b.fuserid
                  join table3 c on b.goodid = c.id
      

  3.   

    select a.name,a.userid,b.goodid,c.name,c.img
    from table1 a,table2 b,table3 c
    where a.userid=b.fuserid and b.goodid= c.id   
      

  4.   

    select table3.name,table3.img
    from table1 inner join table2 on table1.userid=table2.fuserid inner jointable3 on table2.goodid= table3.id  
      

  5.   


    如果有四个表呢,比如
    table4
    (
    id int identity(1,1),
    tid int ,---对应表三的自动编号
    endstr varchar(50)
    )根据第一个表,获取endstr
      

  6.   


    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
      

  7.   


    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
      

  8.   

    select c.name,c.img
    from
     table1 a , table2 b ,table3 c,table4 d 
    where
     a.userid = b.fuserid
    and
     b.goodid = c.id
    and
     d.tid=c.id
      

  9.   


    有几个表就按条件连接几个表,同时注意下 inner join   left join   right join 的逻辑。