表1
ID    T1
1     A
2     B
3     C
表2
ID   Copy_T3   Copy_T1
1    D         A
2    E         B
3    F         C
4    D         B
5    D         C
6    E         C
表3
ID   T3   
1    D
2    E
3    F如何知道表3里的D 查询关联的表1里有什么?LOOP?怎么个写法?或好点的办法~

解决方案 »

  1.   

    select a.* from 表1 a,表2 b,表3 c where a.T1=b.Copy_T1 and b.Copy_T3=c.T3 and c.T3='D'
      

  2.   

    select * from table1 a join (select * from table2 where Copy_T3='d')b on a.t1=b.Copy_T1
      

  3.   

    --和表3没关系了?
    select 表1.* from 表1 where 表1.t1 in (select 表2.Copy_T1 from 表2 where copy3_t3 = 'D')
      

  4.   

    create table a(ID int,T1 varchar(10))
    insert into a values(1,     'A')
    insert into a values(2,     'B')
    insert into a values(3,     'C')
    create table b(ID int,  Copy_T3 varchar(10),  Copy_T1 varchar(10))
    insert into b values(1,    'D',         'A')
    insert into b values(2,    'E',         'B')
    insert into b values(3,    'F',         'C')
    insert into b values(4,    'D',         'B')
    insert into b values(5,    'D',         'C')
    insert into b values(6,    'E',         'C')
    create table c(ID int,T3  varchar(10))
    insert into c values(1,    'D')
    insert into c values(2,    'E')
    insert into c values(3,    'F')
    goselect a.* from a where a.t1 in (select copy_t1 from b where copy_t3 = 'd')
    /*
    ID          T1         
    ----------- ---------- 
    1           A
    2           B
    3           C
    (所影响的行数为 3 行)
    */
    --c表全部
    select a.* from a where a.t1 in (select copy_t1 from b where copy_t3 in (select t3 from c))
    /*
    ID          T1         
    ----------- ---------- 
    1           A
    2           B
    3           C
    (所影响的行数为 3 行)
    */
    drop table a,b,c