Sql Join Query with three tables -


table1

hospitalid   hospitalname                     address     1        andalusia regional hospital      andaluisa     2        baypointe hospital               new york     3        axa hospital                     california 

table2

id   hospitalid   clinicname  1     1          women health  2     1          man health  3     1          travel health  4     1         cardiology  5     2         internal medicine  6     3         cardiology 

table3

id  hospitalid  targethospitalid  distancekm  1      1              2           45  2      1              3           95  3      2              3           15 

i on hospitalid=1 hospital.(in cardiology)

i need find closest hospital here hospital id.

select top 1 targethospitalid  table3  hospitalid = 1  order distancekm 

this code results targethospitalid=2. but, no cardiology department @ baypointe hospital.

i need find nearest hospital cardiology department. how query? help.

you need modify query joining table2 , filtering department.

select top 1 t3.targethospitalid  table3 t3 inner join table2 t2 on t3.targethospitalid = t2.hospitalid t3.hospitalid=1 , t2.clinicname = 'cardiology' order t3.distancekm 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -