我有两张桌子A和B
表A
ID_number as PK
first_name,
L_Name
表B
ID_number,
Email_id,
Flag
我有几个人拥有多个电子邮件ID,并且已在表B上标记为X.
我正在尝试查找拥有电子邮件ID或多个电子邮件ID的人员列表,但从未被标记过。
例如John clark
表B中可能有2封电子邮件,但从未被标记过。
答案 0 :(得分:0)
只需使用not exists
:
select a.*
from a
where not exists (select 1
from b
where b.id_number = a.id_number and b.flag = 'X'
);
答案 1 :(得分:0)
您可能想要执行更新,但您的问题似乎只是关于选择(可能基于选择进行更新)。它应该是这样的:
SELECT A.L_Name
FROM A
WHERE NOT EXISTS (
SELECT 1
FROM B
WHERE B.ID_number = A.ID_number AND B.Flag = 'X'
)
OR
LEFT JOIN
版
SELECT 1
FROM A
LEFT JOIN B ON B.ID_number = A.ID_number AND B.Flag = 'X'
WHER B.ID_number IS NULL
通常,第一个版本比第二个版本快。
答案 2 :(得分:0)
忘记表A ......
SELECT DISTINCT ID_number FROM table_b t1
WHERE NOT EXISTS(
SELECT NULL FROM table_b t2 WHERE t1.ID_number=t2.ID_number AND t2.flag='X'
)
答案 3 :(得分:0)
根据您在评论中的回答判断,我相信这正是您所寻找的:
--drop table update_test;
create table update_test
(
id_num number,
email_id number,
flag varchar2(1) default null
);
insert into update_test values (1, 1, null);
insert into update_test values (1, 2, null);
insert into update_test values (2, 3, null);
insert into update_test values (2, 7, null);
insert into update_test values (3, 2, null);
insert into update_test values (3, 3, 'X');
insert into update_test values (3, 7, null);
select * from update_test;
select id_num, min(email_id)
from update_test
group by id_num;
update update_test ut1
set flag = case
when email_id = (
select min(email_id)
from update_test ut2
where ut2.id_num = ut1.id_num
) then 'X'
else null end
where id_num not in (
select id_num
from update_test
where Flag is not null);
最后一次更新语句将更新并设置Flag
最低id_num
的{{1}}组记录中的email_id
字段。如果id_num
组已将Flag
字段设置为1,则会忽略它。