Oracle获得' IN'中提及的所有记录条款

时间:2014-06-25 09:36:44

标签: oracle oracle10g

我有一个查询,我从前端获取一些字符串,并尝试从数据库中检索一些记录。问题是我添加了一些重复的字符串,这些字符串在检索记录时被省略。我想要我的应用程序重复数据。

SELECT  c.id,c.cptcode,c.cptname,c.patorder,c.ubcode,p.cptprice,u.description
FROM mstcpt c, 
  (select * from mstcptprice
   where hospitalid = 1034 
   and transactionby ='uhc@viamd.com') p ,
  (SELECT * FROM mstub04) u
WHERE c.cptcode IN ('00','70010 - 76499','00400 - 00479','00100 - 00228',
  '00100 - 00228','00400 - 00479','70010 - 76499', '01670','00','00') 
AND c.cptcode = p.cptcode(+)
and c.ubcode=u.code(+)

这些是我从前端得到的字符串:

'00'
'70010 - 76499'
'00400 - 00479'
'00100 - 00228'
'00100 - 00228'
'00400 - 00479'
'70010 - 76499'
'01670'
'00'
'00'

我目前得到的结果是:

ID     CPTCODE        CPTNAME             PATORDER    UB04CODE  CPTPRICE  DESCRIPTION
31287  00100 - 00228  head1               Lab
31288  00400 - 00479  thorax1             Lab
31530  01670          SHOULDER VEIN SURG  Anesthesia  400   
31204  70010 - 76499  diagnostic imaging  Radiology

期望的输出:

ID     CPTCODE        CPTNAME             PATORDER    UB04CODE  CPTPRICE  DESCRIPTION
31287  00100 - 00228  head1               Lab
31287  00100 - 00228  head1               Lab
31288  00400 - 00479  thorax1             Lab
31288  00400 - 00479  thorax1             Lab
31530  01670          SHOULDER VEIN SURG  Anesthesia  400   
31204  70010 - 76499  diagnostic imaging  Radiology
31204  70010 - 76499  diagnostic imaging  Radiology

如何通过我在前端添加它的次数获取所有提到的数据?

2 个答案:

答案 0 :(得分:3)

根据前端的值模拟数据集,而不是将其放入IN条件:

with frontend_values as (
  select '00'            cptcode from dual union all
  select '70010 - 76499' cptcode from dual union all
  select '00400 - 00479' cptcode from dual union all
  select '00100 - 00228' cptcode from dual union all
  select '00100 - 00228' cptcode from dual union all
  select '00400 - 00479' cptcode from dual union all
  select '70010 - 76499' cptcode from dual union all
  select '01670'         cptcode from dual union all
  select '00'            cptcode from dual union all 
  select '00'            cptcode from dual
)
SELECT  
  c.id, c.cptcode, c.cptname, c.patorder, c.ubcode, p.cptprice, u.description
FROM 
  mstcpt                                   c, 
  (
    select * from mstcptprice  
    where hospitalid = 1034 
          and 
          transactionby ='uhc@viamd.com'
  )                                        p ,
  (SELECT * FROM mstub04)                  u ,
  frontend_values                          v 
WHERE 
  c.cptcode = v.cptcode
  and 
  c.cptcode = p.cptcode(+)
  and 
  c.ubcode = u.code(+)

另一种可能性是使用sys.odcivarchar2list类型模拟表格,但不确定这是否适用于Oracle 10g

SELECT  
  c.id, c.cptcode, c.cptname, c.patorder, c.ubcode, p.cptprice, u.description
FROM 
  mstcpt                                   c, 
  (
    select * from mstcptprice  
    where hospitalid = 1034 
          and 
          transactionby ='uhc@viamd.com'
  )                                        p ,
  (SELECT * FROM mstub04)                  u ,
  (
    table(sys.odcivarchar2list(
      '00', '70010 - 76499', '00400 - 00479', '00100 - 00228', '00100 - 00228',
      '00400 - 00479', '70010 - 76499', '01670', '00', '00' 
    ))
  )                                        v 
WHERE 
  c.cptcode = v.column_value
  and 
  c.cptcode = p.cptcode(+)
  and 
  c.ubcode = u.code(+)

答案 1 :(得分:0)

尝试这样的事情:

with t as (
  select level id from dual connect by rownum < 5
), t1 as (select 1 id from dual union all select 1 from dual) 
select t.id
  from t, t1

换句话说,我添加了两个表的笛卡尔积,第一个有4个记录,第二个有两个记录。您必须用数据集替换第一个表。