[英]SQL - Multiple conditions where clause the same column
I have a dataset that looks a little something like this 我有一个看起来像这样的数据集
Subject_ID Diagnosis_ID
001 299
001 288
001 233
001 299
002 299
002 233
003 238
004 299
004 233
I'd like to create a new new table consisting of patients that have diagnosis codes 299 and 233. 我想创建一个新的表格,其中包含诊断代码为299和233的患者。
The code tried so far has been 到目前为止尝试的代码已经
Select *
From mytable
where diagnosis_id = 299 AND diagnosis_id=233
This hasn't worked - 这没用 -
I've also tried 我也试过了
Select *
From mytable
where diagnosis_id = 299
INTERSECT
Select *
From mytable
where diagnosis_id= 233
This hasn't worked either. 这也没有用。
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
you can use IN()... . 你可以使用IN()....
SELECT * FROM TABLE
WHERE diagnosis_id IN(233, 299);
Think group by
and having
: 想
group by
和having
:
Select patient_id
From mytable
where diagnosis_id in (299, 233)
group by patient_id
having count(*) = 2;
Note: If your table can have duplicates, then use count(distinct diagnosis_id) = 2
. 注意:如果您的表可以有重复项,则使用
count(distinct diagnosis_id) = 2
。
Check this:- 检查一下: -
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
Thanks:-) 谢谢:-)
Simple, you can use this : 很简单,你可以使用这个:
Select *
From mytable
where (diagnosis_id = 299 or diagnosis_id=233)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.