SQL Server where 子句上的随机行 [英] SQL Server random rows on where clause

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

问题描述

我需要从表中随机选择 10 行,但必须在 where 子句中完成,因为查询是使用另一个只允许修改这部分的应用程序执行的.

I need to select 10 random rows from a table, but it has to be done in the where clause, because the query is executed using another aplication that only allows to modify this part.

我搜索了很多解决方案(select top 10RAND()ORDER BY NEWID()、...),但在 where 子句中不起作用.

I searched for a lot of solutions (select top 10, RAND(), ORDER BY NEWID(), ...), but none work in the where clause.

有没有办法做到这一点?或某种解决方法?

There an option to do that? or some kind of workaround?

推荐答案

试试这个:

SELECT *
FROM Test 
WHERE Id IN (SELECT TOP 10 Id FROM Test ORDER BY NewId())

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

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