php - How to select from first table if ID exist in second or third table with high efficiency? -


i have 3 tables in assignment. first table: patient: patid, fname, lname, recordnum. patientdr: patid, drid, patientclin: patid, clinid.

i need select patients fname, or lname if "patid" in table "patient" found in table "patientdr" if drid="$docotr->id" or "patientclin" if clinid="$clinic->clinid"

this query

select t1.* patient t1   patlname  '{$search_patlname}%'  , patfname '{$search_patfname}%'  ,  (    exists     (select patid patientdr t2 t2.drid = '{$doctor->id}' , t2.patid = t1.patid)        or  exists      (select patid patientclin t3 t3.clinid = '{$clinic->clinid}' , t3.patid = t1.patid)   )  limit 10 

the number of patients can high, data retrieved using ajax , taking in consideration following note:

"sql statements use exists condition in mysql inefficient since sub-query re-run every row in outer query's table. there more efficient ways write queries, not use exists condition."

is there more efficient way write query, maybe should break rules , insert patient fname , lname in tables patientdr , patientclin reduce response time, or limiting results 10 rows efficient enough ? kind of in advance.

as mentioned in comments, can solved simple inner join:

select t1.*  patient t1 inner join (     select patid      patientdr t2      t2.drid = '{$doctor->id}'      union distinct     select patid      patientclin t3      t3.clinid = '{$clinic->clinid}' ) on t1.patid = i.patid patlname  '{$search_patlname}%'  , patfname '{$search_patfname}%'  limit 10 

if haven't read documentation on inner join, worth taking time so, , play around queries use it.

good luck!


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -