SQL希望从一个表中重复结果为NULL

时间:2015-01-22 16:07:57

标签: sql postgresql

嗨我在SQL查询示例中遇到问题

员工

empid  empname
1      gan
2      sam

指定

id        desig            empid
1      sr officerr          1
2      jr officer           1
3      manager              2

我想要连接表并希望Employee Table重复记录为空

result like

empid     name     desig            id
1         gan      sr officerr      1
1         NULL     jr officer       2
2         sam      manager          3

我正在进行查询,但我没有得到结果

SELECT DISTINCT designatin.empid, employee.empname,designatin.desig 
FROM designatin INNER JOIN employee e ON employee.empid = designatin.empid 
GROUP BY employee.empid, employee.empname, designatin.desig

任何人都可以有解决方案吗?

2 个答案:

答案 0 :(得分:0)

将内部联接更改为左联接:

SELECT DISTINCT designatin.empid, employee.empname,designatin.desig 
FROM designatin LEFT JOIN employee e ON employee.empid = designatin.empid 
GROUP BY employee.empid, employee.empname, designatin.desig

答案 1 :(得分:0)

试试这个,它会帮助你

SELECT e.empid, e.empname,d.desig ,d.id
FROM employee  e
INNER JOIN  Designation d  ON e.empid = d.empid 

请参阅DEMO