如何解决ACCESS中select TOP语句返回全部记录问题?(转)
原文:http://www.tinyu.net/blog/blogview.asp?logID=470
如果在查询TOP语句的后面使用Order by,而且Order by字段中有重复值的话,那么这个TOP很可能会失效,
会返回所有记录
比如:
select top 5 from News order by createDate
如果createDate中有重复值,那么很有可能会显示出所有的记录来,此TOP功能会失效的
但如果createDate中无重复值,那么TOP功能还是有效的
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
因此,在ACCESS中使用TOP功能要注意一下order by的字段是否会有重复值,如果象createDate是"年-月-日 时:分:秒"的,那基本上问题不大的。
但为了保险起见,我们也可以采用"加入主键"的方式:
select top 5 from News order by createDate desc,id desc
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
用主键作"不可能重复"的保障就可以防止此问题出现了!!
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
希望这对某些朋友有用!!
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在网上找到一个相关的解释:
JET SQL不是 T-SQL语句。
jet sql 会返回重复值,也就是说,一个表中如果 ORDER BY 的字段都是 0 ,一共有100条记录,即使你用select TOP 1 来返回记录,也同样返回100条记录,因为 JET DB 无从在这100条记录里面判断先后次序,只能返回100条。要解决此问题可以在后面加入一个主键字段
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)