日期:2014-05-17  浏览次数:20711 次

关于一对多的两张表查询
数据库是oracle
有两个表,结构如下:
    A(a_id, a_name)
    B(b_id, a_id)   
其中,B表通过a_id与A表关联. A与B是一对多的关系
现在希望a,b关联查询查询a的总数,去除重复?
我知道这种方式可以实现:
select count(distinct a.a_id) from A a,B b where a.a_id=b.a_id;

请问还有没有其他写法去除重复???谢谢大家
------解决方案--------------------

--像这种类型的需求一般用exists,而不用关联
select count(*) from A where exists(select 1 from B where A.a_id=B.a_id);

------解决方案--------------------
引用:
SQL code??123--像这种类型的需求一般用exists,而不用关联select count(*) from A where exists(select 1 from B where A.a_id=B.a_id);

多一个条null的。
------解决方案--------------------
你的写法够简单的拉。。。。其他写法就是子查询或者用分析函数来写。。都比你上面写的繁琐阿。。
------解决方案--------------------
group by 
------解决方案--------------------
 select count(a_id)
    from a
   where exists (select 1 from b where a.a_id = b.a_id);