需要显示Last_names
Employees
来自同一部门的Last_name
我该怎么做?
SELECT LAST_NAME, DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID = DEPARTMENTID(&LAST_NAME)
我是怎么尝试的。也许它适用于连接但我真的不知道...... 任何人都可以帮忙??
我的桌子:
应该看起来像(例如姓氏" King")
| LAST_NAME | DEPARTMENT_ID |
| Mueller | 20 |
| Bach | 20 |
答案 0 :(得分:0)
有一个子查询,返回具有该姓氏的员工的部门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
答案 1 :(得分:0)
我添加了两个单引号('
),现在它已经有效了。
SELECT LAST_NAME, DEPARTMENT_ID
FROM EMPLOYEES
WHERE DEPARTMENT_ID IN (select DEPARTMENT_ID
FROM EMPLOYEES
WHERE LAST_NAME = '&LAST_NAME')