简体   繁体   English

如何在一个表中选择两列匹配值的记录?

[英]How to select records with two columns matching values in one table?

I am creating one request form online, where employee generate request in system but his/her boss only able to view those employee's request who are working under him. 我在线创建一个请求表单,员工在系统中生成请求,但他/她的老板只能查看在他下面工作的员工请求。

For ex: In an organization there are various employees are working under 1 boss. 例如:在一个组织中,有不同的员工在1个老板下工作。

---------------------
|EmpNo   |BossNo    |
---------------------
|011     |001       |
|012     |001       |
|013     |001       |
|014     |002       |
---------------------

Here in above table scenario if an employee(012) will generate any request then only boss(001) is able to view that request no one else, same as employee(014) will generate any request then only boss(002) is able to view that request and accept it. 在上面的表格场景中,如果员工(012)将生成任何请求,那么只有boss(001)能够查看该请求没有其他人,与员工(014)相同将生成任何请求然后只有boss(002)能够查看该请求并接受它。

I want to create SQL Query for this..But I am fail to create query for this. 我想为此创建SQL查询。但是我无法为此创建查询。 Here below is the query 以下是查询

select e1.empno, e1.bossno
  from employeedetails as e1
  inner join employeedetails as e2
    on e1.empno= e2.empno
   and e1.bossno= e2.bossno
  group by e1.empno, e1.bossno
  order by e1.empno, e1.bossno

Because, both table retrieve same values then use join as e1.empno= e2.empno and e1.bossno= e2.bossno 因为,两个表都检索相同的值,然后使用join作为e1.empno= e2.empno and e1.bossno= e2.bossno

I think so: 我认同:

select e1AsBoss.empno as idBoss , e2AsEmployer.empno as idEmployer
  from employeedetails as e1AsBoss
  inner join employeedetails as e2AsEmployer
    on e1AsBoss.empno= e2AsEmployer.bossno
  group by e1AsBoss.empno, e2AsEmployer.empno
  order by e1AsBoss.empno, e2AsEmployer.empno

Something like this: 像这样的东西:

SELECT request.Info, request.empno, empDetails.EmployeeName
FROM requestTable as Request
    INNER JOIN employeedetails AS empDetails ON Request.empno = empDetails.empno
WHERE empDetails.bossno = @IdOfLoggedInEmployee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM