![](/img/trans.png)
[英]Trying to join two tables with multiple where condition on both table in sql
[英]SQL Server Join two tables with where condition on both tables
我有兩個表clinical_study和location_countries,他們都有共同的NCT_ID(臨床試驗)字段,我必須從第一個表(性別)應用條件的兩個表中提取數據,然后在第二個表中檢查國家匹配,我成功地設法運行以下查詢,但它在國家領域我正在英國和其他國家
select
clinical_study.NCT_ID, clinical_study.BRIEF_SUMMARY, clinical_study.STUDY_TYPE, clinical_study.GENDER, location_countries.COUNTRY
from clinical_study
inner join location_countries
ON clinical_study.NCT_ID=location_countries.NCT_ID
where clinical_study.GENDER LIKE'Male' or clinical_study.GENDER like 'Both'
and location_countries.COUNTRY ='United Kingdom'
NCT_ID ..... BRIEF_SUMMARY ..... STUDY_TYPE .....性別..... COUNTRY
xys ........ xyz ............... xyz ............兩個.......英國
XYS ........ XYZ ............... XYZ ............男.......法國
xys ........ xyz ............... xyz ............男.......英國
XYS ........ XYZ ............... XYZ ............男.......瑞典
如果我在這里錯過了一個技巧,請你建議嗎?
看起來你需要添加一些括號
select
clinical_study.NCT_ID, clinical_study.BRIEF_SUMMARY, clinical_study.STUDY_TYPE, clinical_study.GENDER, location_countries.COUNTRY
from clinical_study
inner join location_countries
ON clinical_study.NCT_ID=location_countries.NCT_ID
where (clinical_study.GENDER LIKE'Male' or clinical_study.GENDER like 'Both')
and location_countries.COUNTRY ='United Kingdom'
我認為AND優先於OR,所以它將首先進行評估。 因此,您可以使用括號來強制優先級。
where (clinical_study.GENDER LIKE'Male' or clinical_study.GENDER like 'Both')
and location_countries.COUNTRY ='United Kingdom'
你的OR條款需要括號
where (clinical_study.GENDER LIKE'Male' or clinical_study.GENDER like 'Both')
and location_countries.COUNTRY ='United Kingdom'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.