将4个查询合并为1

时间:2015-12-10 13:42:35

标签: sql oracle

我有以下疑问:

Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1890) 
and a.varnum=1
and b.sname = 'cat'
and a.VEXT = b.source_pos_id
and b.posd = c.posid;

Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1977) 
and a.varnum=1
and b.sname = 'dog'
and a.VEXT = b.source_pos_id
and b.posd = c.posid;

Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1832) 
and a.varnum=1
and b.sname = 'mouse'
and a.VEXT = b.source_pos_id
and b.posd = c.posid;

Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1234) 
and a.varnum=1
and b.sname = 'giraffe'
and a.VEXT = b.source_pos_id
and b.posd = c.posid;

如何将上述内容合并为1?

4 个答案:

答案 0 :(得分:1)

Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.varnum=1
and a.VEXT = b.source_pos_id
and b.posd = c.posid

and ((a.VD in(1890) and b.sname = 'cat') OR
     (a.VD in(1977) and b.sname = 'dog') OR
     (a.VD in(1832) and b.sname = 'mouse') OR
     (a.VD in(1234) and b.sname = 'giraffe'));

答案 1 :(得分:1)

你可以UNION ALL他们在一起。

但做这样的事情会更干净:

Select     a.VD
,          a.VEXT
,          c.evalu 
from       val_tb  a
inner join carin b
       on  a.VEXT = b.source_pos_id
inner join mapos c
       on  b.posd = c.posid;
where      a.VA_record_id in (Select reD from val_tb  where valnum = 100008533 and VD=1887) 
       and a.varnum=1
       and ((a.VD in(1890) and b.sname = 'cat')
            or (a.VD in(1977) and b.sname = 'dog')
            or (a.VD in(1832) and b.sname = 'mouse')
            or (a.VD in(1234) and b.sname = 'giraffe'))

另请注意显式join语法。

答案 2 :(得分:1)

一种方法:

Select a.VD, a.VEXT, c.evalu 
from val_tb  a
INNER JOIN carin b
  on a.VEXT = b.source_pos_id
INNER JOIN mapos c
  on b.posd = c.posid
where a.VA_record_id in (Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.varnum=1
and ((b.sname = 'cat' and a.VD in (1890))
 or  (b.sname = 'dog' and a.VD in (1977))
 or  (b.sname = 'mouse' and a.VD in(1832))
 or  (b.sname = 'giraffe' and a.VD in(1234)))

使用工会但比需要复杂得多。

Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1890) 
and a.varnum=1
and b.sname = 'cat'
and a.VEXT = b.source_pos_id
and b.posd = c.posid
UNION ALL
Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1977) 
and a.varnum=1
and b.sname = 'dog'
and a.VEXT = b.source_pos_id
and b.posd = c.posid
UNION ALL
Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1832) 
and a.varnum=1
and b.sname = 'mouse'
and a.VEXT = b.source_pos_id
and b.posd = c.posid
UNION ALL
Select a.VD, a.VEXT, c.evalu from val_tb  a, carin b, mapos c
where a.VA_record_id in 
(Select reD from val_tb  where valnum = 100008533 and VD=1887) 
and a.VD in(1234) 
and a.varnum=1
and b.sname = 'giraffe'
and a.VEXT = b.source_pos_id
and b.posd = c.posid;

答案 3 :(得分:1)

如果值(1890,cat),(1977,dog),......已经存储在数据库中(我会建议或推荐),那么动态确定这些值并使用连接

with data(vd, sname) as
 (select 1977, 'dog' from dual
  union all
  select 1832, 'mouse' from dual
  union all
  select 1890, 'cat' from dual
  union all
  select 1234, 'giraffe' from dual)

Select a.VD, a.VEXT, c.evalu
  from val_tb a, carin b, mapos c, data d
 where a.VA_record_id in (Select reD
                            from val_tb
                           where valnum = 100008533
                             and VD = 1887)
   and a.VD = d.vd
   and a.varnum = 1
   and b.sname = d.sname
   and a.VEXT = b.source_pos_id
   and b.posd = c.posid;