[英]SQL - Multiple conditions where clause the same column
我有一个看起来像这样的数据集
Subject_ID Diagnosis_ID
001 299
001 288
001 233
001 299
002 299
002 233
003 238
004 299
004 233
我想创建一个新的表格,其中包含诊断代码为299和233的患者。
到目前为止尝试的代码已经
Select *
From mytable
where diagnosis_id = 299 AND diagnosis_id=233
这没用 -
我也试过了
Select *
From mytable
where diagnosis_id = 299
INTERSECT
Select *
From mytable
where diagnosis_id= 233
这也没有用。
select Subject_ID from (
Select Distinct Subject_ID, Diagnosis_ID
From
Table_1
Where Diagnosis_ID=299 or Diagnosis_ID=288
)
Group By Subject_ID
Having count(Subject_ID)>=2
你可以使用IN()....
SELECT * FROM TABLE
WHERE diagnosis_id IN(233, 299);
想group by
和having
:
Select patient_id
From mytable
where diagnosis_id in (299, 233)
group by patient_id
having count(*) = 2;
注意:如果您的表可以有重复项,则使用count(distinct diagnosis_id) = 2
。
检查一下: -
Select subject_id
from
(
Select distinct subject_id, diagnosis_id
from
mytable
where diagnosis_id in ('299','233')
) a
group by subject_id
having count(*)=2
谢谢:-)
很简单,你可以使用这个:
Select *
From mytable
where (diagnosis_id = 299 or diagnosis_id=233)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.