表A 字段 a
表B 字段 b,c 
如果我想去查询表A中a 的值在表B中的b,c 刚好匹配的数据。
SQL应该怎么写呢?如A的值有 1,2,3
B的值 b,c
      1,2
      3,4我想把表A中的1查出来

解决方案 »

  1.   

    根据上述描述    table_a.a = table_b.b or  table_a.a = table_b.c但是,按逻辑, 2、3 也匹配上了,不止一个 1啊。楼主麻烦再说清楚,到底要什么结果,什么又是不要的。
      

  2.   

    我的意思是B表只有两个值,一个是1,2 (对应列为a,b)
                        另一个是3,4
    这样的话2和3没匹配啊,有匹配的只有1,2啊
      

  3.   

    scott@TBWORA> create table a(a varchar2(100));表已创建。scott@TBWORA> create table b(b varchar2(20), c varchar2(20));表已创建。scott@TBWORA> insert into a(a) values('1,2,3');已创建 1 行。scott@TBWORA> insert into b(b,c) values('1','2');已创建 1 行。scott@TBWORA> insert into b(b,c) values('3','4');已创建 1 行。scott@TBWORA>
    scott@TBWORA> commit;提交完成。scott@TBWORA>
    scott@TBWORA> select b.b, b.c
      2  from b
      3  where exists (select 1 from a
      4                 where a.a||',' like b.b||','||'%'
      5                   and a.a||',' like b.c||','||'%' );未选定行scott@TBWORA>
    scott@TBWORA> select b.b, b.c
      2  from b
      3  where exists(select 1 from a
      4                where instr(a.a||',',b.b||','||b.c)>=1
      5                   or instr(a.a||',',b.c||','||b.b)>=1 );B                                        C
    ---------------------------------------- ----------------------------------------
    1                                        2
      

  4.   

    scott@TBWORA> -- 更准确的写法:
    scott@TBWORA> select b.b, b.c
      2  from b
      3  where exists(select 1 from a
      4                where instr(a.a||',',b.b||','||b.c||',')>=1
      5                   or instr(a.a||',',b.c||','||b.b||',')>=1 );B                                        C
    ---------------------------------------- ----------------------------------------
    1                                        2
      

  5.   

    额。我数据的值都是随便写的,实际上数据会比这个多,也没那么简单。你这SQL是不是把数据的值给限定死了啊~~