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

琐事的子查询

热度:15   发布时间:2016-04-24 06:39:26.0
繁琐的子查询
查询两个表A,B中的某个字段 对该字段求和   
并对这两个表的两个和取差
并且这个差值必须小于另一个表C中的某个字段的值



然后把这些作为一个条件传给D表去查询满足条件的结果

------解决方案--------------------
引用:
Quote: 引用:

列出 你的表结构, 数据结构,以及想要查询出的结果,那样大家就更容易针对性的解答了




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

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

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

create table d (
  a_id number(4),
  d_area number(32)
);

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

传name到a表查出a_id用id去c中查b_id然后去b表中查出b_area并求和
同时a_id 闯入d表中查询d_id并求和
然后再将两个和求差,并要求差值小于e表中valname为test的value值
然后在将满足以上条件的a表中的信息返回


select t1.a_id, t1.name, t1.s1 - t2.s2
  from (select a.a_id, a.name, sum(b.b_area) s1
          from a, b, c
         where a.a_id = c.a_id
           and c.b_id = b.id
           and a.name = p_name
         group by a.a_id, a.name) t1,
       (select a.a_id, a.name, sum(d.d_area) s2
          from a, d
         where a.a_id = d.a_id
           and a.name = p_name
         group by a.a_id, a.name) t2,
       (select value from e where e.valname = 'test') t3
 where t1.a_id = t2.a_id
   and t1.s1 - t2.s2 < t3.value
  相关解决方案