需要显示在同一部门工作的Employees
Last_names
,例如Last_name
我该怎么做?
SELECT LAST_NAME, DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID = DEPARTMENTID(&LAST_NAME)
我就是这样尝试的。也许它适用于连接,但我真的不知道......谁能帮忙??
我的桌子:
- Empoyees (Emp_ID, LAST_NAME, DEPARTMENT_ID)
- 部门(DEPARTMENT_ID、位置)
应该看起来像(考试姓氏"国王")
| LAST_NAME | DEPARTMENT_ID |
| Mueller | 20 |
| Bach | 20 |
有一个子查询,该查询返回具有该姓氏的员工的部门 ID。IN
让其他员工来自同一部门。
SELECT LAST_NAME, DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (select DEPARTMENT_ID
FROM EMPLOYEES
WHERE LAST_NAME = &LAST_NAME)
根据您使用的 dbms,JOIN
版本可能具有更好的性能:
SELECT e1.LAST_NAME, e1.DEPARTMENT_ID
FROM EMPLOYEES e1
JOIN (select DEPARTMENT_ID
FROM EMPLOYEES
WHERE LAST_NAME = &LAST_NAME) e2
ON e1.DEPARTMENT_ID = e2.DEPARTMENT_ID
我添加了 2 个单引号 ( '
),现在它起作用了。
SELECT LAST_NAME, DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (select DEPARTMENT_ID
FROM EMPLOYEES
WHERE LAST_NAME = '&LAST_NAME')