2014-06-25 47 views
0

我有一個查詢,我從前端獲得一些字符串,並嘗試從數據庫中檢索一些記錄。問題是我添加了一些重複的字符串,在檢索記錄時被忽略。我希望重複數據以及我的應用程序。Oracle獲取'IN'條款中提到的所有記錄

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 ='[email protected]') 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 

我怎樣才能得到所有的數據是我與我在前端添加它的次數相提並論?

+0

假設你的查詢實際上有'in'而不是'like'。我想我理解你的意思,但是你能否展示理想的結果;或者最好創建一個更簡單的測試用例來證明這個問題?以及如何從前端傳遞字符串 - 作爲綁定變量,嵌入查詢中的數組? –

+0

謝謝,現在改變了。 – Suman

+0

你說,如果'IN'子句具有相同的值_twice_,你也希望結果_duplicated_? 'IN'在邏輯上形成多個'OR'條件,所以可能是唯一的值列表。因此,使用這些值生成一個視圖並將它們與您的表結合起來。 –

回答

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 ='[email protected]' 
)          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 ='[email protected]' 
)          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(+) 
+0

'odcivarchar2list'選項也適用於10g。尼斯。 –

+0

+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條記錄,以及第二個有兩個記錄。您必須將第一個表格替換爲您的數據集。

+0

我沒有得到如何解決這個問題。 – Suman

+0

這是一個例子,作者需要用他的數據源替換t,並且他的所有行將被複制 – zaratustra

+0

但是,這將顯示每行兩次,而不是基於每個單獨字符串出現在列表中的次數來重複行嗎? –