如何在四个表

时间:2015-04-24 06:57:50

标签: sql left-join

SELECT * FROM
  mtc_employee_master EB,
  mtc_employee_education_master EE,
  mtc_employee_experience_master EEXP,
  mtc_employee_salary_master ES 
WHERE
  EB.id=16 AND 
  EE.emp_tbl_id=EB.id AND 
  EEXP.emp_tbl_id=EB.id AND 
  ES.emp_tbl_id=EB.id

如果数据在所有表中都可用,则此方法可以正常工作。我想从mtc_employee_master获取记录,就像主表一样。

我已成功通过以下查询加入两个表 -

SELECT * FROM 
  mtc_employee_master EB 
LEFT JOIN 
  mtc_employee_education_master EE
ON
  EB.`id` = EE.`emp_tbl_id` 
WHERE
  EB.id=16

3 个答案:

答案 0 :(得分:2)

试试这个:

  SELECT * FROM mtc_employee_master EB
  left join mtc_employee_education_master EE on EE.emp_tbl_id=EB.id
  left join mtc_employee_experience_master EEXP on  EEXP.emp_tbl_id=EB.id 
  left join mtc_employee_salary_master ES on ES.emp_tbl_id =EB.id
  WHERE EB.id=16 

答案 1 :(得分:0)

这样做(已编辑):

 SELECT EB.*
 FROM mtc_employee_master EB left join 
 (SELECT EE.id var FROM mtc_employee_education_master EE 
  join  mtc_employee_experience_master EEXP on EE.id=EEXP.id
  join mtc_employee_salary_master ES 
  on ES.id=EEXP.id) temp
  on EB.id= temp.var
 where  EB.id=16;

答案 2 :(得分:0)

SELECT * FROM
  mtc_employee_master EB,
  mtc_employee_education_master EE,
  mtc_employee_experience_master EEXP,
  mtc_employee_salary_master ES 
WHERE
  EB.id=16 AND 
  IFNULL(EE.emp_tbl_id,0)IN(EB.id,0) AND 
  IFNULL(EEXP.emp_tbl_id,0)IN(EB.id,0) AND 
  IFNULL(ES.emp_tbl_id,0)IN(EB.id,0)