0

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

这是我当前的查询,它只检查 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;

我明白了

 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我想在字段空白的情况下输出的行

我怎样才能做到这一点?

4

2 回答 2

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
于 2013-08-19T12:00:10.747 回答
0

怎么换

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

经过

AND (myo_maps_study ~ 'MYO[0-9]*\$' OR myo_maps_study = '')
于 2013-08-19T10:03:40.867 回答