如何在四个表之间应用 MySQL 中的左连接
How to apply left join in MySQL between four tables
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
如果所有 table 中的数据都可用,则此方法工作正常。我想像大师一样从 mtc_employee_master
获取记录 table。
我成功地用下面的查询连接了两个 tables-
SELECT * FROM
mtc_employee_master EB
LEFT JOIN
mtc_employee_education_master EE
ON
EB.`id` = EE.`emp_tbl_id`
WHERE
EB.id=16
试试这个:
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
这样做(已编辑):
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;
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)
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
如果所有 table 中的数据都可用,则此方法工作正常。我想像大师一样从 mtc_employee_master
获取记录 table。
我成功地用下面的查询连接了两个 tables-
SELECT * FROM
mtc_employee_master EB
LEFT JOIN
mtc_employee_education_master EE
ON
EB.`id` = EE.`emp_tbl_id`
WHERE
EB.id=16
试试这个:
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
这样做(已编辑):
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;
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)