mysql一个表两个字段关联另一张表的一个字段

做两次关联

select
   M.id,M.task_name,M.plan_date_start,M.plan_date_end,M.project_id,M.center_id,M.stage_id,M.parent_stage_id,
   t2.stage_name,
	 t3.stage_name
FROM
   (SELECT id,task_name,plan_date_start,plan_date_end,project_id,center_id,stage_id,parent_stage_id FROM  ctc_task where project_id='1'  AND (a.center_id is null or a.center_id='') UNION select a.id,a.task_name,a.plan_date_start,a.plan_date_end,a.project_id,a.center_id,a.stage_id,a.parent_stage_id from ctc_task a ,ctc_task_connect b where b.task_id=a.id and a.project_id='1' AND (a.center_id is null or a.center_id='')) as M
INNER JOIN ctc_stage AS t2 ON M.stage_id = t2.id
INNER JOIN ctc_stage AS t3 ON M.parent_stage_id = t3.id

你可能感兴趣的:(sql,数据库)