简体   繁体   English

SQL-Oracle:解决基本问题的难度,第2部分

[英]SQL-Oracle: Difficulties in resolving basic problems, part2

Problem: The HR department needs a query that prompts the user for an employee last name. 问题:人力资源部门需要一个查询,提示用户输入雇员的姓氏。 The query then displays the last name and hire date of any employee in the same department as the employee whose name they supply(excluding that employee). 然后查询显示与提供其姓名的员工(不包括该员工)在同一部门的任何员工的姓氏和雇用日期。 For example, if the user enters Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey). 例如,如果用户输入Zlotkey,则查找使用Zlotkey的所有员工(Zlotkey除外)。

I managed to do this so far, but i have no clue how to finish it. 到目前为止,我设法做到了,但是我不知道如何完成它。 For now it shows me all employees after i write the last_name excluding it. 现在,它向我显示所有我写上不包括姓氏的雇员。 Any suggestions please how to continue? 有什么建议请如何继续?

SELECT last_name, TO_CHAR(hire_date,'DD-MON-YYYY') AS "HIRE_DATE"
FROM   employees
WHERE  last_name <>ALL (SELECT '&last_name'
                         FROM  employees)

AND    department_id  IN  (SELECT department_id
                           ???....

PS: This problem is from the Oracle tutorials (Oracle Database 11g: SQL Fundamentals 1 of 1 (year 2009), Practice 7 , exercise 1 for people who already done this:). PS:此问题来自Oracle教程(Oracle数据库11g:SQL基础知识1 of 1(2009年),练习7,对于已经这样做的人进行练习1 :)。

Something like this? 像这样吗

SELECT last_name, TO_CHAR(hire_date,'DD-MON-YYYY') AS "HIRE_DATE"
FROM   employees a
JOIN (Select department_id from employees where last_name = :surname) b on a.department_id = b.department_id
and last_name <> :surname

EDIT The only problem with this type of solution is that if there are two people with the same surname in different departments, so it might be useful to maybe use something like an employee number as a filter instead of surname. 编辑这种类型的解决方案的唯一问题是,如果在不同部门中有两个姓氏相同的人,那么使用诸如雇员编号之类的名称作为过滤器来代替姓氏可能会很有用。

SELECT  LAST_NAME, HIRE_DATE 
FROM EMPLOYEES
WHERE DEPARTMENT_ID= (SELECT DEPARTMENT_ID 
FROM EMPLOYEES
WHERE LAST_NAME LIKE '&NAME')
AND LAST_NAME <> '&NAME';

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

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