表1
id  name
1   A1
2   B1
3   C1
4   D1
表2
id  name Bid
1   A2    1
2   B2    1
3   C2    2
4   D2    2表3
Bid  name 
1    A3
2    B3表1的id对应表2的id,表2的Bid对应表3的Bid。现在要根据表3的Bid得到表1的name也就是说,表1的name可以分为两类,一类Bid=1,一类Bid=2。

解决方案 »

  1.   

    select * from table1 where table3.bid=table2.bid and table2.id=table1.id
      

  2.   

    create table tb1(id int, name char(2))
    insert tb1 select 1,   'A1'
    union all select 2,   'B1'
    union all select 3,   'C1'
    union all select 4,   'D1'create table tb2(id int, name char(2), Bid int)
    insert tb2 select 1,   'A2',    1
    union all select 2,   'B2',    1
    union all select 3,   'C2',    2
    union all select 4,   'D2',    2create table tb3(Bid int, name char(2))
    insert tb3 select 1,    'A3'
    union all select 2,    'B3'select * from tb1, tb2, tb3 where tb1.id=tb2.id and tb2.Bid=tb3.Bid--result
    id          name id          name Bid         Bid         name 
    ----------- ---- ----------- ---- ----------- ----------- ---- 
    1           A1   1           A2   1           1           A3
    2           B1   2           B2   1           1           A3
    3           C1   3           C2   2           2           B3
    4           D1   4           D2   2           2           B3(4 row(s) affected)
      

  3.   

    select t.bid ,表1.name from (
    select 表2.id,表3.bid from 表2,表3 where 表2.bid = 表3.bid ) t,表1
    where t.id = 表1.id
      

  4.   

    select a.*
    from 
      表1 a 
      inner join 表2 b on a.id=b.id
      inner join 表3 c on b.bid=c.bid
    where 
      c.bid =参数