日期:2014-05-19  浏览次数:20369 次

为什么下面的sql语句 用 not in 可以出结果, not exists 不能
语句如下  

一.  
SELECT   top                     2   user_Integral_tbl.user_Integral_customers_id,

sum(   user_Integral_tbl.user_Integral_Numerical)   as   总成绩    

FROM   user_Integral_tbl  


where       not   exists     (SELECT   top   2   user_Integral_tbl.user_Integral_customers_id     FROM   user_Integral_tbl   GROUP   BY   user_Integral_tbl.user_Integral_customers_id   ORDER   BY     sum(   user_Integral_tbl.user_Integral_Numerical)     DESC)
GROUP   BY   user_Integral_tbl.user_Integral_customers_id

ORDER   BY   总成绩   DESC

二.
SELECT   top                     2   user_Integral_tbl.user_Integral_customers_id,

sum(   user_Integral_tbl.user_Integral_Numerical)   as   总成绩    

FROM   user_Integral_tbl  


  where     user_Integral_tbl.user_Integral_customers_id   not   in   (SELECT   top   2   user_Integral_tbl.user_Integral_customers_id     FROM   user_Integral_tbl   GROUP   BY   user_Integral_tbl.user_Integral_customers_id   ORDER   BY     sum(   user_Integral_tbl.user_Integral_Numerical)     DESC)

GROUP   BY   user_Integral_tbl.user_Integral_customers_id

ORDER   BY   总成绩   DESC

------解决方案--------------------
因为一里面的not exists(子查询),子查询总是有结果返回,如果子查询没有结果返回,同样的查询也没有结果返回
------解决方案--------------------
修改

一.
SELECT top 2 user_Integral_tbl.user_Integral_customers_id,

sum( user_Integral_tbl.user_Integral_Numerical) as 总成绩

FROM user_Integral_tbl


GROUP BY user_Integral_tbl.user_Integral_customers_id

ORDER BY 总成绩 DESC