拆分此查询

时间:2016-09-19 10:29:14

标签: sql

    $sql= "
SELECT tbl_doctors. * , 
       tbl_specialisation. * , 
       tbl_doctor_address.addr_id, 
       tbl_address .  * 
FROM `tbl_doctors` INNER JOIN
     `tbl_specialisation`
     ON ( tbl_doctors.dr_spec_id = tbl_specialisation.spec_id ) INNER JOIN
     (tbl_doctor_address JOIN
      tbl_address
      ON tbl_doctor_address.addr_id = tbl_address.addr_id
     )
     ON tbl_doctors.dr_id = tbl_doctor_address.dr_id  
WHERE tbl_doctors.dr_id = '$id'";

1 个答案:

答案 0 :(得分:0)

我想这部分需要改造:

(tbl_doctor_address JOIN
  tbl_address
  ON tbl_doctor_address.addr_id = tbl_address.addr_id
 )
 ON tbl_doctors.dr_id = tbl_doctor_address.dr_id

就像这样:

INNER JOIN `tbl_doctor_address` ON tbl_doctors.dr_id = tbl_doctor_address.dr_id
INNER JOIN `tbl_address` ON tbl_doctor_address.addr_id = tbl_address.addr_id

如果没有为医生设置地址,您可以使用LEFT JOIN

LEFT JOIN `tbl_doctor_address` ON tbl_doctors.dr_id = tbl_doctor_address.dr_id
LEFT JOIN `tbl_address` ON tbl_doctor_address.addr_id = tbl_address.addr_id

告诉我它是否有帮助?