【问题标题】:Using a SQL correlated sub-query with exists operator使用带有存在运算符的 SQL 相关子查询
【发布时间】:2021-03-07 02:03:54
【问题描述】:

我正在尝试通过使用带有存在运算符的相关子查询来查找 job_history 表中的所有记录,其部门 ID 与雇员表中的部门 ID 不同,但是,我收到了错误代码:

ORA-00933:SQL 命令未正确结束

有人可以根据下面的架构和代码告诉我我做错了什么吗?

Select * 
from Job_History as J 
where Exists(Select * from hr.Employees e where e.Department_ID != J.Department_ID);

【问题讨论】:

    标签: sql oracle subquery where-clause


    【解决方案1】:

    这里:

    from Job_History as J 
    

    Oracle 不支持使用关键字as 为表命名(仅在列别名中支持)。

    您也很可能希望在子查询中的 employee_id 上使用关联子句。

    所以:

    select * 
    from job_history j 
    where exists (
        select 1 
        from hr.employees e 
        where e.employee_id = j.employee_id and e.department_id <> j.department_id
    );
    

    【讨论】:

    • 非常感谢您的帮助。我可以通过将 hr 添加到下面的 job_history 来得到答案。很抱歉没有提到我正在使用 Oracle SQL,因此我在选择表时必须添加 HR。见下文: select * from hr.job_history j where exists (select 1 from hr.employees e where e.employee_id = j.employee_id and e.department_id j.department_id);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多