2013-08-19 58 views
0

我正在寫一個與數據庫交互的perltk gui。如果myo_maps_study存在,我希望我的查詢的某個條件是字段myo_maps_study應該匹配正則表達式MYO [0-9] * \ $。它可能永遠不會存在的一些行。如果myo_maps_study不存在,我仍然想得到結果使用Perl的Psql查詢

這是我當前的查詢,它只檢查MYO [0-9] * \ $正則表達式。

my $query = " 
     SELECT wtg_uid, pat_first_name, pat_last_name, pat_chi_no, 
     wtg_scantype, wtg_confirmed, 
     std_in_out_patient, 
     inc_uid, wtg_appeared, 
     myo_maps_study, myx_event, 
     inc_incident_required, inc_incident_done, 
     inc_gp_letter_required, inc_gp_letter_done, 
     inc_reappt_required, inc_reappt_done, 
     inc_comment 
     FROM ((waiting INNER JOIN patients ON (wtg_pat_uid = pat_uid)) 
     LEFT OUTER JOIN studies ON (wtg_uid = std_wtg_uid) 
     LEFT OUTER JOIN myx ON (std_uid = myx_std_uid) 
     LEFT OUTER JOIN myo on (std_uid = myo_std_uid)) 
     LEFT OUTER JOIN incidents ON (wtg_uid = inc_wtg_uid) 
     WHERE wtg_scan_date = '$date' AND myo_maps_study ~ 'MYO[0-9]*\$' 
     ORDER BY pat_last_name;"; 

回報

17291 | AR   | MAX  | 1609 | E-R AND RNVG |    | O     |   | Y   | MYO130430  | 
     |      |     |      |     |      |     | 
    17201 | ANN  | MCK  | 3011 | E-R AND RNVG |    | O     |   | Y   | MYO134416  | 
     |      |     |      |     |      |     | 

當我刪除了myo_maps_study條件

SELECT wtg_uid, pat_first_name, pat_last_name, pat_chi_no,wtg_scantype, wtg_confirmed, std_in_out_patient, inc_uid, wtg_appeared, myo_maps_study, myx_event,inc_incident_required, inc_incident_done,inc_gp_letter_required, inc_gp_letter_done,inc_reappt_required, inc_reappt_done,inc_comment FROM ((waiting INNER JOIN patients ON (wtg_pat_uid = pat_uid)) LEFT OUTER JOIN studies ON (wtg_uid = std_wtg_uid) LEFT OUTER JOIN myx ON (std_uid = myx_std_uid) LEFT OUTER JOIN myo on (std_uid = myo_std_uid)) LEFT OUTER JOIN incidents ON (wtg_uid = inc_wtg_uid) WHERE wtg_scan_date = '19/08/13' ORDER BY pat_last_name; 

我得到第二個查詢AR MAX和ANN MCK出現兩次

17264 | KIS  | ASH  | 150 | E-R AND RNVG |    |     |   | Y   |    | 
     |      |     |      |     |      |     | 
    17262 | WIL  | BE   | 1301 | E-R AND RNVG |    |     |   |    |    | 
     |      |     |      |     |      |     | 
    17268 | ELI  | HAR  | 2105 | E-R AND RNVG |    |     |   |    |    | 
     |      |     |      |     |      |     | 
    17291 | AR   | MAX  | 1609 | E-R AND RNVG |    | O     |   | Y   | MYO130430T  | 
     |      |     |      |     |      |     | 
    17291 | AR   | MAX  | 1609 | E-R AND RNVG |    | O     |   | Y   | MYO130430  | 
     |      |     |      |     |      |     | 
    17201 | ANN  | MCK  | 3011 | E-R AND RNVG |    | O     |   | Y   | MYO134416T  | 
     |      |     |      |     |      |     | 
    17201 | ANN  | MCK  | 3011 | E-R AND RNVG |    | O     |   | Y   | MYO134416  | 
     |      |     |      |     |      |     | 
    17351 | SHI  | MUL  | 2907 | R-R ONLY  |    |     |   |    |    | 
     |      |     |      |  

通知。我期望的輸出是介於兩者之間的。我希望每個人在每次查詢時只出現一次。即如果存在myo_maps_study,則它必須是格式爲MYO[0-9]*\$的格式。對於那些沒有myo_maps_study的人,我想要將該行與空白輸出

我該怎麼做?

回答

1
select distinct on (pat_last_name, pat_first_name) 
    wtg_uid, pat_first_name, pat_last_name, pat_chi_no, 
    wtg_scantype, wtg_confirmed, 
    std_in_out_patient, 
    inc_uid, wtg_appeared, 
    myo_maps_study, myx_event, 
    inc_incident_required, inc_incident_done, 
    inc_gp_letter_required, inc_gp_letter_done, 
    inc_reappt_required, inc_reappt_done, 
    inc_comment 
from 
    (
     waiting 
     inner join 
     patients on wtg_pat_uid = pat_uid 
     left outer join 
     studies on wtg_uid = std_wtg_uid 
     left outer join 
     myx on std_uid = myx_std_uid 
     left outer join 
     myo on std_uid = myo_std_uid 
    ) 
    left outer join incidents on wtg_uid = inc_wtg_uid 
where 
    wtg_scan_date = '$date' 
    and 
    (myo_maps_study ~ 'myo[0-9]*\$' or myo_maps_study is null) 
order by pat_last_name 
+0

非常感謝 – moadeep

0

如何通過

AND (myo_maps_study ~ 'MYO[0-9]*\$' OR myo_maps_study = '') 
+0

更換

AND myo_maps_study ~ 'MYO[0-9]*\$' 

我嘗試這樣做,它給我的輸出作爲第一個查詢相同。我認爲這是因爲加入條件'LEFT OUTER JOIN myo on(std_uid = myo_std_uid))'會失敗,因爲在那時沒有MYO存在。雖然它可能會在當天晚些時候做 – moadeep