我有两张表a
id name code
2 a 1001
3 aa 1001
4 aaa 1001
5 aaaa 1001
6 b 1001
7 bb 1001
表B
id plan
2 qer
5 wrq
6 eq
7 eer
8 qer
9 qe
10 qeq
11 erq我要两张表关联查询得到的结果如下:
id name code plan
2 a 1001 qer
3 aa 1001
4 aaa 1001
5 aaaa 1001 wrq
6 b 1001 eq
7 bb 1001 eer
注意:plan值空的要保留。

解决方案 »

  1.   

    左外连接。
    select a.id,a.name,a.code,b.plan from a,b
    where a.id=b.id(+);
      

  2.   

    select a.id,a.name,a.code,b.plan
    from a left jon b on a.id=b.id
      

  3.   

    如果再加一个条件也就是过滤掉plan 列值qer也就是结果为
    id name code plan 
    2 a     1001  
    3 aa    1001 
    4 aaa   1001 
    5 aaaa  1001 wrq 
    6 b     1001 eq 
    7 bb    1001 eer 这样该怎么做
      

  4.   

    提问时请一次把问题说清楚,不要等别人给了人答案,再有新的需求出来,这样会没完没了的。
    select a.id,a.name,a.code,c.plan
    from a left jon (select * from b where plan!='qer') c on a.id=c.id
      

  5.   

    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
      

  6.   

    select a.id,a.name,a.code,b.plan from a,b 
    where a.id=b.id(+) and b.plan!='qer';
      

  7.   

    select a.id,a.name,a.code,b.plan from a,b 
    where a.id=b.id(+) and b.plan!='qer';
      

  8.   

    I F L U。
    被你打败了,挤牙膏么。
    表达能力非常欠缺你。
    算了,好事做到底吧。select a.id,a.name,a.code,decode(b.plan,'qer',null) plan from a,b 
    where a.id=b.id(+);
      

  9.   

    被你忽悠晕了,漏掉了最重要的部分。应该是:select a.id,a.name,a.code,decode(b.plan,'qer',null,b.plan) plan from a,b 
    where a.id=b.id(+);
      

  10.   

    select id,name,code plan from a,b where a.id=b.id(+);