SQL Server 随机排序 [英] SQL Server Random Sort

查看:40
本文介绍了SQL Server 随机排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在存储过程中将 sql 查询的结果排序为随机顺序的最佳方法是什么?

What is the best way to sort the results of a sql query into a random order within a stored procedure?

推荐答案

这是 SO# 19412.这是我在那里给出的答案:

This is a duplicate of SO# 19412. Here's the answer I gave there:

select top 1 * from mytable order by newid()

在 SQL Server 2005 及更高版本中,您可以使用 TABLESAMPLE 来获取可重复的随机样本:

In SQL Server 2005 and up, you can use TABLESAMPLE to get a random sample that's repeatable:

SELECT FirstName, LastName FROM Contact TABLESAMPLE (1 ROWS) ;

这篇关于SQL Server 随机排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆