当前位置: 代码迷 >> Oracle开发 >> 琐碎的子查询2
  详细解决方案

琐碎的子查询2

热度:57   发布时间:2016-04-24 06:39:11.0
繁琐的子查询2
数据结构是这样的


create table A (
  id number(4);
  name varchar2(32)
);

create table B(
  id number(4),
  area number(32);
);

create table c (
  a_id number(4),
  B_id number(4)
);

create table d (
  a_id number(4),
  num1 number(32),
  num2 number(32)
);

create table e(
  name varchar(32),
  value number(18)
);


其中传给表a一个name会返回多条a_id 
把a_id传给c
求找到表b中符合条件的个数


同时a_id也传给表d
求表d中num1和num2分别符合一个条件的个数

如果上面两个个数相等就返回表a的数据
------解决方案--------------------
引用:
Quote: 引用:

Quote: 引用:



如果上面两个个数相等就返回表a的数据


这句话怎么理解:

求表d中num1和num2分别符合一个条件的个数


是不是把(select  distinct p_name from a)来替换p_name?


select distinct t3.id, t3.name
  from a t3,
       (select t1.id
          from (select a.id, count(c.b_id) c_count
                  from a, c
                 where a.id = c.a_id
                 group by a.id) t1,
               (select a.id, count(d.a_id) d_count
                  from a, d
                 where a.id = d.a_id
                   and d.num1 = 'tiaojian1'
                   and d.num2 = 'tiaojian2'
                 group by a.id) t2
         where t1.id = t2.id
           and t1.c_count = t2.d_count) t4
 where t3.id = t4.id
  相关解决方案