2013-11-28 102 views
1

我有兩個表clinical_study和location_countries,他們都有NCT_ID(臨牀試驗)領域的共同點,我必須從兩個表中拉數據應用條件在第一個表(性別),然後又檢查了第二個表的國家比賽, 我成功設法運行下面的查詢,但其在鄉間田野我得到英國和其他國家SQL Server加入兩個表與哪裏條件在這兩個表上

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 ..... GERER ..... COUNTRY

xys ........ xyz ............... xyz .. ..........都.......英國

xys ........ xyz ............... xyz ........... .Male .......法國

xys ........ xyz ............... xyz .......... ..Male .......英國

xys ........ xyz ............... xyz ........ ....男.......瑞典

請你的意見,如果我在這裏缺少一個竅門

回答

2

它看起來像你需要添加一些括號

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' 
1

我認爲AND優先於OR,因此它將首先被評估。所以你可以使用括號來強制優先。

where (clinical_study.GENDER LIKE'Male' or clinical_study.GENDER like 'Both') 
    and location_countries.COUNTRY ='United Kingdom' 
+0

這工作,這是支架,謝謝大家 –

1

您需要括號角落找尋你的OR子句

where (clinical_study.GENDER LIKE'Male' or clinical_study.GENDER like 'Both') 
and location_countries.COUNTRY ='United Kingdom'