簡體   English   中英

SQL - 多個條件where子句相同的列

[英]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 byhaving

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM