A表字段如下:
id b c
1  2 3
2  2 2
3  3 2
4  1 4B表字段如下:
id name
1  aa
2  bb
3  cc
4  ddA表中的字段 b和c与B表中的id相对应,也就是A表字段b的2对应B表name中的bb,也就是A表字段c的3对应B表name中的cc,
现在我想得到的是A表中字段b和c在B表中name相同的记录,怎么做到,也就是说按上面的表来说应该得到
A表
id  b  c
1   2  3
2   2  2
3   3  2

解决方案 »

  1.   

    就是我现在想要表A中的b字段和c字段相等的记录
    上表中得出的结果应该是
    id  b  c 
    1  2  3 
    2  2  2 
    3  3  2 
      

  2.   

    呵呵,根据楼主的结果来判断你B表的内容可能应该是这样的:id name 
    1  aa 
    2  bb 
    3  bb 
    4  dd不知道我理解得是否正确~ 
      

  3.   

    我是这样写的
    select * from A where id in(select id from A t1,At2 where t1.b=t2.c)
    得到的不是我想要的结果
    我想得到最后的结果是
    A表
    id  b  c 
    1   2  3 
    2   2  2 
    3   3  2 
      

  4.   

    A表字段如下: 
    id b c 
    1  2 3 
    2  2 2 
    3  3 2 
    4  1 4 B表字段如下: 
    id name 
    1  aa 
    2  bb 
    3  bb
    4  dd A表中的字段 b和c与B表中的id相对应,也就是A表字段b的2对应B表name中的bb,也就是A表字段c的3对应B表name中的cc, 
    现在我想得到的是A表中字段b和c在B表中name相同的记录,怎么做到,也就是说按上面的表来说应该得到 
    A表 
    id  b  c 
    1  2  3 
    2  2  2 
    3  3  2  CREATE TABLE tb (id int,b int,c int)
     INSERT INTO tb(id,b,c) VALUES (1,2,3)
     INSERT INTO tb(id,b,c) VALUES (2,2,2)
     INSERT INTO tb(id,b,c) VALUES (3,3,2)
     INSERT INTO tb(id,b,c) VALUES (4,1,4)
     
     CREATE TABLE tb2(id int,[name] varchar(200))
     INSERT INTO tb2(id,[name]) VALUES (1,'aa')
     INSERT INTO tb2(id,[name]) VALUES (2,'bb')
     INSERT INTO tb2(id,[name]) VALUES (3,'bb')
     INSERT INTO tb2(id,[name]) VALUES (4,'dd')
     
     SELECT tmp.id,tmp.b,tmp.c FROM 
     (SELECT tb.id AS id,b,c,[name] FROM tb LEFT JOIN tb2 ON tb.b=tb2.id) AS tmp  JOIN (
      SELECT * FROM (SELECT tb.id,c,[name] FROM tb LEFT JOIN tb2 ON tb.c=tb2.id) AS tm)  tmp2  ON tmp.id=tmp2.id AND tmp.[name]=tmp2.[name]
     ORDER BY id
        DROP table tb
      DROP TABLE tb2
      
    得到的结果id  b  c 
    1  2  3 
    2  2  2 
    3  3  2 
    注意我标注颜色的地方,不知道是不是楼主有写错的~。
      

  5.   

    你可以用select * from A where id in(select id from A t1,At2 where t1.b=t2.c)这种方式 给我写出来吗
      

  6.   

    你写的我也明白了,现在我要的就是不用B表了,那样太不好做了,得到的结果还是一样的
    只是现在,你看我的这条语句是这样写的
    A表字段如下: 
    id b c 
    1  2 3 
    2  2 2 
    3  3 2 
    4  1 4 
    select * from A where id in(select id from A t1,At2 where t1.b=t2.c) 我想得到最后的结果是 
    A表 
    id  b  c 
    1  2  3 
    2  2  2 
    3  3  2 
      

  7.   


    declare @ta table (id int,b int, c int)
    declare @tb table (id int,name varchar(10))insert @ta
    select 1,2,3 union all 
    select 2,2,2 union all 
    select 3,3,2 union all
    select 4,1,4insert @tb
    select 1,'aa' union all 
    select 2,'bb' union all 
    select 3,'bb' union all 
    select 4,'dd'select d.id,b,c from 
    (select a.id,b,b.id as id1,name from @ta a,@tb b where a.b=b.id )d
    join
    (select a.id,c,c.id as id1,name from @ta a,@tb c where a.c=c.id)e
    on d.id=e.id and d.name=e.name
    order by d.id
    id    b    c
    ----------------
    1     2    3
    2     2    2
    3     3    2
      

  8.   

    感觉你的逻辑有点混乱了,如果你去掉了B表,应该在A表中增加字段吧? 要不然你之前的题设都不成立了...
    你又如何得到你后面想要的结果呢??? 
    B表中的NAME 你是打算如何处理的呢?
      

  9.   


    在你的表A中增加了2个字段,这样 SQL 语句就变成:
      select id,b,c from a where nameb=namec
      

  10.   

    select * from A where id in(select id from A t1,At2 where 
    t1.id<>t2.id and (t1.b=t2.c or t2.b=t1.c))
    只用A表一个表,这样就可以得出我要的结果了
    你可以去试试,呵呵,谢谢你了~~