两个关系A与B,一对多。
A包含关键字n,B包含字段n和m,n与m可决定B。
n可取值a,b,c,……
m可取值0,1,2,……
求一SQL语句,查两表联立后m不为0的n。

解决方案 »

  1.   

    select a.n
      from a a, b b
     where a.n = b.n
       and b.m <> 0
     group by a.n
      

  2.   

    select a.n from a,
    (elect distinct n from b 
    minus
    select distinct n from b where m=0) b
    where a.n=b.n
      

  3.   

    要看lz的需求了
    还有就是,b表的n 是不是a表的n子集
    如果是的话,就不需要关联a表了,这样速度会更快
      

  4.   

    select * from a where exists (select 1 from b where n=a.n and m<>0)