有两个表A,B结构相同,索引也相同,都有一个BMBH,RYBH两个字段.
A表比B表的数据要多,B表中的数据都在A中.BMBH,RYBH是两个表的主键.
怎么可以查询出A表比B表多的数据来啊.

解决方案 »

  1.   

    select a.* from a
    where not exists (select BMBH from b where a.BMBH=b.BMBH)
    or not exists (select RYBH from b where a.RYBH=b.RYBH)
    你先试下这个SQL
      

  2.   

    不行啊,我表A的数据是
    BMBH       RYMC
    ---------- ----------
    001        001
    002        002
    003        003
    004        004
    005        005
    表B的数据是
    BMBH       RYMC
    ---------- ----------
    001        001
    002        002
    003        003
    004        004
    005        005
    001        002
    002        003
    003        004
    004        005
    005        006
    想把B表中最后五行的数据查出来啊.
      

  3.   

    select bmbh,rymc from 
    (select bmbh,rymc from test1
    union
    select bmbh,rymc from test2)
    group by bmbh,rymc
     having(count(*)) = 1
    ;
    这样也不可以.
      

  4.   

    怎么感觉你说的有些矛盾呢?b表中的数据都在a表中,怎么还能查出b表中的最后五行数据,而且还是a表比b表多出来的数据?我没看明白
      

  5.   

    select b.BMBH,b.RYBH from b
    where b.BMBH,b.RYBH not in (
    select b.BMBH,RYBH from a,b
    where b.BMBH = a.BMBH
    and b.RYBH = a.RYBH)
    是这个意思么?
      

  6.   

    select bmbh,rymc from 
    (select bmbh,rymc,'a' as c from a
    union
    select bmbh,rymc,'b' as c from a)
    group by bmbh,rymc
     having(count(*)) = 1
    ;
    这样,不好意思啊,刚想出来.
      

  7.   

    感觉你写的SQL和你的需求不是1回事啊!
    用他写的啊!
     twenty_three(god is a girl) ( ) 信誉:100    Blog   加为好友  2007-04-16 15:09:50  得分: 0
      

  8.   

    twenty_three(god is a girl)他写的不行啊,语法就有错误啊.
      

  9.   

    select b.BMBH,b.RYBH from b
    where not exists(
    select b.BMBH,RYBH from a,b
    where b.BMBH = a.BMBH
    and b.RYBH = a.RYBH)
    要是不行就笨点的方法
    select b.BMBH,b.RYBH from b
    where b.BMBH not in (
    select b.BMBH from a,b
    where b.BMBH = a.BMBH
    and b.RYBH = a.RYBH)
    and b.RYBH not in  (
    select b.BMBH from a,b
    where b.BMBH = a.BMBH
    and b.RYBH = a.RYBH)
      

  10.   

    select bmbh,rymc from  b
    where bmbh||rymc not in
    (select bmbh||rymc from a)
      

  11.   

    select * from test
    minus
    select * from test2;
      

  12.   

    有这么麻烦吗?如下不是很简单吗?
    SELECT * FROM b WHERE (bmbh,rymc)  NOT IN(SELECT bmbh,rymc FROM a)
      

  13.   

    select bmbh,rybh from a
    minus
    select bmbh,rhbh from b
      

  14.   

    select * from b  minus  select * from a;
    刚学会的