随笔

SQL SERVER中

1、去掉重复数据用到distinct,取前5个用到top   5,随机产生这5个记录需要用到order   by   newid(),怎么能解决?

 

sql语句如下

select   top   5   a.*   from   (select   distinct   *   from   t1)   a   order   by   newid()

 

2、sql 语句中distinct和top是否可以同时出现?

可以,但是distinct要放在top之前,如下

select distinct top 5 * from a order by oid desc

 

原文地址:https://www.cnblogs.com/liancs/p/3879350.html