使用perl进行Psql查询

时间:2013-08-19 09:41:32

标签: sql perl postgresql

我正在编写一个与数据库交互的perltk gui。我希望在我的查询中有一个条件,如果myo_maps_study存在,字段myo_maps_study应匹配正则表达式MYO [0-9] * \ $。某些行可能永远不存在。如果myo_maps_study不存在,我仍然希望得到结果

这是我当前的查询,它仅检查MYO [0-9] * \ $ regex。

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;

我得到了

 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     |               |                    |         |              |                |   
        |                       |                   |                        |     

注意第二个查询AR MAX和ANN MCK出现两次。我想要的输出介于两者之间。我希望每个人每次查询只出现一次。即如果myo_maps_study存在,则它必须是格式为MYO[0-9]*\$的那个。对于那些没有myo_maps_study的人,我希望输出的行包含字段空白

我该怎么做?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如何更换

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

通过

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