首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

关于一对多的两张表查询解决办法

2012-12-30 
关于一对多的两张表查询数据库是oracle有两个表,结构如下:A(a_id, a_name)B(b_id, a_id)其中,B表通过a_id

关于一对多的两张表查询
数据库是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);

热点排行