Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 选择一个表中存在但另一个表中缺少的值的最佳方法?神谕_Sql_Oracle - Fatal编程技术网

Sql 选择一个表中存在但另一个表中缺少的值的最佳方法?神谕

Sql 选择一个表中存在但另一个表中缺少的值的最佳方法?神谕,sql,oracle,Sql,Oracle,我的解决方案: 表名:HW1_人,HW1_sfaff SELECT I.ID,person.person_id FROM HW1_PERSON LEFT JOIN HW1_STAFF ON I.ID=person.person_id WHERE person.person_id IS NULL; 第5行错误:ORA-00904:“PERSON”。“PERSONID”:无效标识符 数据示例: INSERT INTO hw1_person (id, first_name, last_

我的解决方案:
表名:HW1_人,HW1_sfaff

SELECT I.ID,person.person_id
FROM HW1_PERSON
LEFT JOIN HW1_STAFF
ON     
I.ID=person.person_id
WHERE person.person_id IS NULL;  
第5行错误:ORA-00904:“PERSON”。“PERSONID”:无效标识符

数据示例:

INSERT INTO hw1_person (id, first_name, last_name, dob, address) VALUES (26, 'fname44', 'lname44', to_date('2/12/1990', 'MM/DD/YYYY'), '301 6th Av NJ');
INSERT INTO hw1_person (id, first_name, last_name, dob, address) VALUES (27, 'fname45', 'lname45', to_date('12/8/1982', 'MM/DD/YYYY'), '975 7th Av NJ');

INSERT INTO hw1_staff (person_id, emp_num, start_date, end_date, salary) VALUES (1, 'WZIAE', to_date('7/1/1965', 'MM/DD/YYYY'), null,70000);
INSERT INTO hw1_staff (person_id, emp_num, start_date, end_date, salary) VALUES (2, 'EWEMU', to_date('11/18/1980', 'MM/DD/YYYY'), to_date('12/26/1970', 'MM/DD/YYYY'),80000);
INSERT INTO hw1_staff (person_id, emp_num, start_date, end_date, salary) VALUES (3, 'KJHSN', to_date('2/26/1991', 'MM/DD/YYYY'), null,40000);                                          

您可以使用
不存在

select *
from hw1_staff s
where not exists (
        select 1
        from hw1_person p
        where p.id = s.person_id
        )
它将从staff中选择所有行,这些行在person表中没有对应的行

或者,您可能想要相反的结果,即所有非员工的人员:

select *
from hw1_person p
where not exists (
        select 1
        from hw1_staff s
        where p.id = s.person_id
        )
select *
from hw1_person p
left join hw1_staff s
    on p.id = s.person_id
where s.person_id is null
如果要使用基于加入的备选方案获取非员工的人员:

select *
from hw1_person p
where not exists (
        select 1
        from hw1_staff s
        where p.id = s.person_id
        )
select *
from hw1_person p
left join hw1_staff s
    on p.id = s.person_id
where s.person_id is null

你在询问中犯了一个错误

你写道:

FROM HW1_PERSON
LEFT JOIN HW1_STAFF
但要使查询正常工作,必须:

FROM HW1_PERSON I
LEFT JOIN HW1_STAFF person

如另一个答案所述;是的,您可以使用
NOT EXISTS
来完成任务。

非常感谢,选择1是什么意思?选择1只是为了满足需要“选择某个东西”的语法。@LeeJohnny-
NOT EXISTS
不关心选择的是什么。它只是检查是否有行。因此,我们提供一个常量1,而不是任何或所有列,谢谢much@LeeJohnny-作为OP,你有权接受任何你想要的答案,但我可以问你为什么不接受我的答案吗?另一个答案是在7分钟后说出我的答案。你能再回答我一个问题吗?