當我運行4個不同的查詢時,我收到了一些無意義的結果。SQL查詢中的無意義結果
最主要的一條是:
SELECT distinct p.id
FROM praxis px,basis_medikation bm,therapie_zyklus_medikation tzm,therapie_zyklus tz,therapie t,patient p, behandlung b
WHERE 1=1
AND EXISTS (select * from diagnose d, diagnose_mamma dm, tumor_status ts where d.patient_id = p.id and d.diagnose_mamma_id = dm.id and d.deleted = 0
and ts.tumorstatus_m in ('M1')
and d.datum < t.datum
and (ts.id = dm.tumorstatus_id or ts.id = dm.tumorstatus_rechts_id))
AND px.ID = p.praxis_ID AND px.testpraxis = 0 AND tz.deleted = 0 and tzm.deleted = 0 and t.deleted = 0 AND p.ID = t.patient_ID AND t.ID = tz.therapie_id AND tz.ID = tzm.therapiezyklus_id AND tzm.basis_medikation_id = bm.ID
AND t.datum >= to_date('2009-10-01', 'yyyy-MM-dd') AND t.datum < to_date('2013-10-01', 'yyyy-MM-dd')
AND t.behandlung_id = b.id
AND b.deleted = 0
AND (
(
bm.atcaname in ('Medizin1','Medizin2','Medizin3','Medizin4')
AND bm.product_name <> 'Medizin0'
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5')
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6')
)
OR (
bm.atcaname in ('Medizin1','Medizin2','Medizin3','Medizin4')
AND bm.product_name <> 'Medizin0'
AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5')
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6')
)
OR (
bm.atcaname in ('Medizin1','Medizin2','Medizin3','Medizin4')
AND bm.product_name <> 'Medizin0'
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5')
AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6')
)
OR (
bm.atcaname not in ('Medizin1','Medizin2','Medizin3','Medizin4')
AND bm.product_name = 'Medizin0'
AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5')
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6')
)
OR (
bm.atcaname not in ('Medizin1','Medizin2','Medizin3','Medizin4')
AND bm.product_name = 'Medizin0'
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5')
AND EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6')
)
OR (
bm.atcaname not in ('Medizin1','Medizin2','Medizin3','Medizin4')
AND bm.product_name = 'Medizin0'
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin5')
AND NOT EXISTS (select * from basis_medikation bm2, therapie_zyklus_medikation tzm2, therapie_zyklus tz2 where tz2.therapie_id = tz.therapie_id AND tz2.ID = tzm2.therapiezyklus_id AND tzm2.basis_medikation_id = bm2.ID and bm2.atcaname = 'Medizin6')
)
) ;
它選擇的乳腺癌患者已被處理,要麼Medizin1或Medizin2或Medizin3或僅Medizin4,和那些誰合併任何與Medizin5或Medizin6這些藥物的和那些誰一直在與Medizin0和Medizin5治療,和那些誰一直在與Medizin0和Medizin6和那些誰一直只與Medizin0治療治療...
對於第二個查詢我只添加條件
AND b.line = 1
對於第三查詢我補充一下:
AND b.line = 2
而對於第四:
AND b.line > 2
而且廢話來當結果第一次查詢的數量比的總和低其他三個查詢的結果...應該等於或少於...我認爲...
但的確我缺少一些東西...
每當您編寫單個SQL查詢並且它不再適合一個屏幕時,您應該停下來思考如果無法以更優雅的方式解決問題。 – Philipp
您是否檢查某些返回的行是否重複?對所有三個查詢進行聯合查找。 – dnoeth
如果沒有真正看到數據庫中的數據,真的很難猜測解決方案對於這個問題的影響。我會仔細看看你的數據,以確保'b.line'具有你期望的值。 – woemler