Oracle 表中的随机行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7750396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:22:04  来源:igfitidea点击:

Oracle random row from table

oraclerandomrow

提问by Adrian

I found this solution for selecting a random row from a table in Oracle. Actually sorting rows in a random manner, but you can fetch only the first row for a random result.

我找到了这个解决方案,用于从 Oracle 的表中选择随机行。实际上以随机方式对行进行排序,但您只能获取随机结果的第一行。

SELECT *
FROM table
ORDER BY dbms_random.value;

I just don't understand how it works. After ORDER BY it should be a column used for sorting. I see that "dbms_random.value" returns a value lower than zero. This behavior can be explained or is just like that?

我只是不明白它是如何工作的。ORDER BY 之后应该是用于排序的列。我看到“dbms_random.value”返回一个小于零的值。这种行为可以解释还是就是这样?

Thanks

谢谢

回答by Kevin Burton

you could also think of it like this:

你也可以这样想:

SELECT col1, col2, dbms_random.value
FROM table
ORDER BY 3

In this example the number 3 = the third column

在本例中,数字 3 = 第三列

回答by Aitor

When you order by dbms_random.value, Oracle orders by the expression, not for a column.For every record Oracle calculate a random number, and then order by this number.

当你按dbms_random.value 排序时,Oracle 按表达式排序,而不是针对一个列。Oracle 为每条记录计算一个随机数,然后按这个数字排序。

In a similar way, is like this:

以类似的方式,是这样的:

select * from emp order by upper(ename);

You have an order by based on a function.

您有一个基于函数的 order by。