我在Postgres上工作,下面的查詢似乎永遠不會執行,但它只是最終的OR導致問題,它運行良好,如果我註釋掉它。這僅僅是一種低效的寫作方式嗎?如果是這樣,我想不出一個替代方案。SQL Multiple OR BETWEENS
SELECT a.subject_id, a.hadm_id, a.admittime, d.icd9_code, l.short_title, d.seq_num
FROM mimiciii.admissions a
INNER JOIN mimiciii.diagnoses_icd d
ON a.subject_id = d.subject_id
AND a.hadm_id = d.hadm_id
INNER JOIN mimiciii.d_icd_diagnoses l
ON d.icd9_code = l.icd9_code
WHERE a.subject_id IN
(SELECT DISTINCT d.subject_id
FROM mimiciii.diagnoses_icd d
WHERE d.icd9_code BETWEEN '390%' and '459%')
AND d.icd9_code NOT IN
(SELECT d.icd9_code
FROM mimiciii.diagnoses_icd d
WHERE d.icd9_code BETWEEN 'V01%' AND 'V91%'
OR d.icd9_code BETWEEN 'E000%' AND 'E999%'
OR d.icd9_code BETWEEN '630%' AND '679%'
OR d.icd9_code BETWEEN '760%' AND '999%'
)
ORDER BY subject_id, admittime
謝謝,但通配符正在工作。 – Sam
@Sam。 。 。他們沒有做你認爲他們在做的事情。他們不是通配符;他們只是'%',這對任何閱讀查詢的人(包括將來的你)都會產生誤導。 –