Sqlserver 中exists 和 in
如图,现在有两个数据集,左边表示#tempTable1,右边表示#tempTable2。现在有以下问题:
1.求两个集的交集?
2.求tempTable1中不属于集#tempTable2的集?
先创建两张临时表:
create table #tempTable1 ( argument1 nvarchar(50), argument2 varchar(20), argument3 datetime, argument4 int ); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher001','13023218757',GETDATE()-1,1); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher002','23218757',GETDATE()-2,2); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher003','13018757',GETDATE()-3,3); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher004','13023257',GETDATE()-4,4); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher005','13023218',GETDATE()-5,5); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher006','13023218',GETDATE()-6,6); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher007','13023218',GETDATE()-7,7); insert into #tempTable1(argument1,argument2,argument3,argument4) values('preacher008','13023218',GETDATE()-8,8); create table #tempTable2 ( argument1 nvarchar(50), argument2 varchar(20), argument3 datetime, argument4 int ); insert into #tempTable2(argument1,argument2,argument3,argument4) values('preacher001','13023218757',GETDATE()-1,1); insert into #tempTable2(argument1,argument2,argument3,argument4) values('preacher0010','23218757',GETDATE()-10,10); insert into #tempTable2(argument1,argument2,argument3,argument4) values('preacher003','13018757',GETDATE()-3,3); insert into #tempTable2(argument1,argument2,argument3,argument4) values('preacher004','13023257',GETDATE()-4,4); insert into #tempTable2(argument1,argument2,argument3,argument4) values('preacher009','13023218',GETDATE()-9,9);
比如,我现在以#tempTable1和#tempTable2的argument1作为参照
1.求两集的交集:
1)in 方式
select * from #tempTable2 where argument1 in (select argument1 from #tempTable1)
2)exists 方式
select * from #tempTable2 t2 where exists (select * from #tempTable1 t1 where t1.argument1=t2.argument1)
2.求tempTable1中不属于集#tempTable2的集
1)in 方式
select * from #tempTable1 where argument1 not in (select argument1 from #tempTable2)
2)exists 方式
select * from #tempTable1 t1 where not exists (select * from #tempTable2 t2 where t1.argument1=t2.argument1)
3)exists对应数据的行索引
例如现在有两张表tbl_customer,tbl_phone,其中一个客户对应一个或多个电话信息,在维护电话号码时,往往会显示例如客户姓名、客户性别、修改人、修改时间等等点,查看选中的客户电话详细记录时可以查看该客户名义下的所有电话号码信息;因为是维护的客户电话号码,所以这里的修改人和修改时间应该针对的是当前客户对应的所有电话号码的最新修改时间,那么在提取这些数据时,可以使用 max() 和 exists获取这一个特别数据。
create table tbl_phone
(
id number(12) not null,
customer_id number(12),
phonenumber varchar(20),
isdeleted char(1),
syscreatedatetime date,
syscreater number(12),
syslastmodifytime date,
syslastmodifier number(12)
(
id number(12) not null,
customer_id number(12),
phonenumber varchar(20),
isdeleted char(1),
syscreatedatetime date,
syscreater number(12),
syslastmodifytime date,
syslastmodifier number(12)
)
create table tbl_customer
(
id number(12) not null,
name varchar(50),
gender char(1),
isdeleted char(1),
syscreatedatetime date,
syscreater number(12),
syslastmodifytime date,
syslastmodifier number(12)
(
id number(12) not null,
name varchar(50),
gender char(1),
isdeleted char(1),
syscreatedatetime date,
syscreater number(12),
syslastmodifytime date,
syslastmodifier number(12)
)
查询语句这样写:
select distinct c.name,
code(c.gender, '1', '男', '0', '女', null) as gender,
(select syslastmodifytime
from tbl_phone
where exists (select max(t.syslastmodifytime)
from tbl_vip_phone t
where t.customer_id = a.id)
and rownum = 1) as syslastmodifytime,
(select syslastmodifier
from tbl_phone
where exists (select max(t.syslastmodifytime)
from tbl_vip_phone t
where t.customer_id = a.id)
and rownum = 1) as syslastmodifier
from tbl_customer c
join tbl_phone p
on c.isdeleted = 0
and p.isdeleted = 0
and c.id = p.customer_id
(select syslastmodifytime
from tbl_phone
where exists (select max(t.syslastmodifytime)
from tbl_vip_phone t
where t.customer_id = a.id)
and rownum = 1) as syslastmodifytime,
(select syslastmodifier
from tbl_phone
where exists (select max(t.syslastmodifytime)
from tbl_vip_phone t
where t.customer_id = a.id)
and rownum = 1) as syslastmodifier
from tbl_customer c
join tbl_phone p
on c.isdeleted = 0
and p.isdeleted = 0
and c.id = p.customer_id