A
----
id  name  Bid
1   A1    1
2   A2    1
3   A3    2
4   A4    2
5   A5    2B
----
Bid  name Cid
1    B1   1
2    B2   1
3    B3   2 
4    B4   2
5    B5   2A 的 Bid 对应 B 的 Bid
select * from A where Bid in (select Bid from B where Cid = 1)
现在知道 A 的Bid = 1,求出 B 的Cid = 多少?
可以看出Cid = 1,但我想用一条sql语句求出来,如何写?

解决方案 »

  1.   

    select cid from b where bid=1
    ?
      

  2.   

    知道A的Bid=1,推出B的Cid ?select Cid from B where Bid=1   --A的bid=1,就是B的bid=1
      

  3.   

    create table A(id int,   name char(2),  Bid int)
    insert A select 1,   'A1',    1
    union all select 2,   'A2',    1
    union all select 3,   'A3',    2
    union all select 4,   'A4',    2
    union all select 5,   'A5',    2create table B(Bid int,  name char(2), Cid int)
    insert B select 1,    'B1',   1
    union all select 2,    'B2',   1
    union all select 3,    'B3',   2 
    union all select 4,    'B4',   2
    union all select 5,    'B5',   2select * from A, B
    where A.Bid=B.Bid and A.Bid=1--result
    id          name Bid         Bid         name Cid         
    ----------- ---- ----------- ----------- ---- ----------- 
    1           A1   1           1           B1   1
    2           A2   1           1           B1   1(2 row(s) affected)
      

  4.   

    select b.cid from a,b where a.bid = b.bid and a.bid = 1