postgres加入查询

时间:2012-05-28 04:55:01

标签: sql postgresql join

我有3个postgresql表如下

员工

emp_name | emp_id | user_name | psw | etc

工作

Work_id |work_type| Project_manger_id(employee) | Architect_id (emp) | tech_lead_id | dept_id

dept

dept_id | dept_name 

现在我想要的是像这样的表

work_type | project_manager (name)  | architect (name) | tech_lead (name ) | dept (name )

简单地说,我想要的是获取工作表,但用名称

替换id

1 个答案:

答案 0 :(得分:3)

试试这个

Select WT.Work_Type,PM.emp_name As ProjectManager,AR.emp_name AS Arhitect,TL.emp_name As  TechLead,Dept.Dept_name As Department From
Work 
INNER JOIN employees AS PM ON (Wt.Project_manger_id=PM.emp_id)
INNER JOIN employees AS AR ON (Wt.Architect_id=AR.emp_id)
INNER JOIN employees AS TL ON (Wt.tech_lead_id=TL.emp_id)
INNER JOIN Dept As Dept ON (Wt.dept_id=Dept.dept_id)