Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 为什么“除了”可以过滤掉ID/行,而“不在”则不起作用?_Sql Server_Tsql - Fatal编程技术网

Sql server 为什么“除了”可以过滤掉ID/行,而“不在”则不起作用?

Sql server 为什么“除了”可以过滤掉ID/行,而“不在”则不起作用?,sql-server,tsql,Sql Server,Tsql,为什么Exception在过滤出ID/行时起作用,而不是在不起作用时起作用?当我运行此代码时,我要过滤掉的患者id仍在结果中 Select pm.patient_id from PatientMedication pm Where (pm.medicine_id = 11 or pm.medicine_id = 29) and pm.patient_id not in Select distinct patient_id from PatientMedication pm Wh

为什么Exception在过滤出ID/行时起作用,而不是在不起作用时起作用?当我运行此代码时,我要过滤掉的患者id仍在结果中

Select pm.patient_id
from PatientMedication pm
Where (pm.medicine_id = 11 or pm.medicine_id = 29)
and pm.patient_id not in 
    Select distinct patient_id from PatientMedication pm
    Where (pm.medicine_id = 11 or pm.medicine_id = 29)
    and pm.start_date < '2009-03-17' 
但当我运行此程序时,预期的人员将按我的意愿过滤掉

Select pm.patient_id
   from PatientMedication pm
Where pm.medicine_id = 11 or pm.medicine_id = 29
except
    Select distinct patient_id from PatientMedication pm
    Where (pm.medicine_id = 11 or pm.medicine_id = 29)
    and pm.start_date < '2009-03-17' 

谢谢。

这就是你想要实现的目标吗

Select pm.patient_id 
from PatientMedication pm 
Where (pm.medicine_id = 11 or pm.medicine_id = 29) 
AND pm.start_date >= '2009-03-17'

如果用括号括住子查询,会发生什么情况?类似于。。。而且pm.patient_id不在select distinct中…使用同一别名两次是不好的做法。即使使用适当的@Racso,您的括号建议也给出了与使用except选项相同的答案。事实上,没有导致错误的原因。哎呀,对我来说。糟糕的代码。我想是的,但我也认为问题集中在不在的明显怪异行为上。