sql语句查询的时候not in 里面查询为空 则整个都为空 求高手

2024-12-25 17:05:54
推荐回答(4个)
回答1:

select distinct(u.UserGuid)
from ComPany as c,
Users as u
where u.UserGuid not in (select c.UserGuid from ComPany as c,Users as u where DATEDIFF(month,c.DengTime,'2012-03-01')=0 and c.UserGuid=u.UserGuid group by c.UserGuid)
and u.IsDelete!='2' and u.UserType='2'
首先:确定下面两个查询有值
1:
select c.UserGuid from ComPany as c,Users as u where DATEDIFF(month,c.DengTime,'2012-03-01')=0 and c.UserGuid=u.UserGuid group by c.UserGuid
2:
select distinct(u.UserGuid)
from ComPany as c,
Users as u
where u.IsDelete!='2' and u.UserType='2'

回答2:

看看用 not exists 代替 not in 行不行?

select
distinct(u.UserGuid)
from
ComPany as c,Users as u
where
not exists
( select 1
from
ComPany as c2, Users as u2
where
DATEDIFF(month, c2.DengTime,'2012-03-01')=0
and c2.UserGuid = u2.UserGuid
and u.UserGuid = u2.UserGuid
)
and u.IsDelete!='2'
and u.UserType='2'

回答3:

为什么要用这个呢 not in 呢 !
最低效率的程序 执行,当然写是方便

回答4:

为什么要用这个呢 not in 呢 !最低效率的程序 执行,当然写是方便