SQL查询语句SELECT中带有case when嵌套子查询判断的问题

这是我现在在项目中遇到的问题,有两个表a和b,用case when判断a表中的某一字段的每一个值在b表中的某一字段值中是否存在可以实现吗,我现在知道的是用“in”好像不行。各位高手帮帮忙啊,给个详细点的答案好吗?

1、创建两张测试表

create table test_case1(id number, value varchar2(200));

create table test_case2(id number, value varchar2(200));

2、先在表1中插入测试数据

insert into test_case1 values(1,'a');

insert into test_case1 values(2,'b');

insert into test_case1 values(3,'c');

insert into test_case1 values(4,'d');

insert into test_case1 values(5,'e');

commit;

3、在表2中插入数据

insert into test_case2 values(1,'aa');

insert into test_case2 values(2,'bb');

insert into test_case2 values(3,'cc');

insert into test_case2 values(6,'ee');

commit;

4、两表关联,并编写case when的语句

select t.*,

       case when b.id is not null then '存在' else '不存在' end flag

 from TEST_CASE1 t, TEST_CASE2 b

 where t.id = b.id(+)

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-28

判定A表的数据是否存在B表,如果存在则显示存在,不存在则显示不存在

例如S#存在于SC表和student表中,判定s#是否都在student表中存在存在则显示存在,不存在则显示不存在,具体如下:

from student 

select s#, 

case when   s# in(select s# from sc) then '存在'

when s# not in( select s# from sc) then  '不存在'

end

from student 

本回答被网友采纳
第2个回答  2017-08-03
select * from a where EXISTS(select id from b where a.相同的字段=b.相同的字段)

第3个回答  2017-07-09
问题是什么?
第4个回答  2014-12-03
疑问点:你整个的意图是什么?单单两张表的两个字段 ,T_A 表的 a 字段 在 T_B 表中 b 字段中是否存在?可以具体一些么? 至少 你可以说明 你想实现的思维意图是什么?追问

对,是你说的那样,我就是要判断a表中a字段在b表中的b字段里存不存在,然后给它一个标示符flag我要用这个flag在后面的程序里判断

本回答被网友采纳
相似回答