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'
看看用 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'
为什么要用这个呢 not in 呢 !
最低效率的程序 执行,当然写是方便
为什么要用这个呢 not in 呢 !最低效率的程序 执行,当然写是方便