大家好,我有一条语句,可以执行,只是感觉写的很搓,请教高手优化下现在有两个表:Table1、Table2 ,Table2中记录了Table1和其他表中的关系
需求:根据关键字TAB1_ID,查找不在Table2表中,而在Table1中的数据如下:Table1
{TAB1_ID,NAME,...
1,a;
2,b
3,c
4,d
5,e
}Table2
{TAB2_ID,TAB1_ID,...
1,2
2,3
3,4
}select a.TAB1_ID
from Table1 a
where a.TAB1_ID in(3,4,5)
and a.TAB1_ID not in (select b.TAB1_ID
from Table2 b
where b.TAB1_ID in (3,4,5))上面用了in 和not in,感觉写的不怎么对头,请高手帮优化下啊,不胜感激~~
需求:根据关键字TAB1_ID,查找不在Table2表中,而在Table1中的数据如下:Table1
{TAB1_ID,NAME,...
1,a;
2,b
3,c
4,d
5,e
}Table2
{TAB2_ID,TAB1_ID,...
1,2
2,3
3,4
}select a.TAB1_ID
from Table1 a
where a.TAB1_ID in(3,4,5)
and a.TAB1_ID not in (select b.TAB1_ID
from Table2 b
where b.TAB1_ID in (3,4,5))上面用了in 和not in,感觉写的不怎么对头,请高手帮优化下啊,不胜感激~~
select *
from table1 a,table2 b
where a.tab1_id in (3,4,5) and and b.tab_id in (3,4,5) a.tab1_id<>b.tab1_id
不过这样明显没有结果集吧条件1和2 同3没有任何交集吧
from Table1 a
where a.TAB1_ID in(3,4,5)
and not exists (select 1 from Table2 where TAB1_ID in (3,4,5) and TAB1_ID=a.TAB1_ID)