我找不到正确的sql查询以解决以下问题
这是表格
+------+----------+---------+-------+
| id | empname | superid | CTC |
+------+----------+---------+-------+
| 1 | Sujatha | 2 | 56000 |
| 2 | Rajesh | 3 | 60000 |
| 3 | Vasanthi | 4 | 65000 |
| 4 | Murali | 5 | 75000 |
+------+----------+---------+-------+
我应该打印empname
和supervisor
名称。 supervisor
也是一名员工。
我尝试了几个查询但找不到答案。请帮忙。
我尝试格式化表格,但此平台不允许
答案 0 :(得分:1)
您可以通过SELF JOIN
SELECT id AS employeeid
,e.empname
,s.empname AS supervisor
,e.CTC AS employeeCTC
FROM Employees AS e
INNER JOIN Employees AS s ON e.superid = s.id
答案 1 :(得分:0)
你可以这样做(如果你的表名是“mytable”例如):
SELECT emp_table.empname, super_table.empname
FROM mytable emp_table
INNER JOIN mytable super_table ON emp_table.id = super_table.superid
答案 2 :(得分:0)
让表名为员工 这是查询:
SELECT e1.empname, e2.empname
FROM employee AS e1 INNER JOIN employee AS e2
ON e1.superid=e2.id
答案 3 :(得分:0)
尝试此查询。
Select empname
, supervisorName
from employee, supervisor
where employee.empId=supervisor.empId AND
employee.superId=supervisor.superId
答案 4 :(得分:0)
SELECT
em.empname,
sp.empname
FROM emp_super em
INNER JOIN emp_super sp
ON em.id = sp.superid