2 数据库中唯一的 RAND() 项

发布于 2024-10-28 19:37:48 字数 169 浏览 6 评论 0原文

目前我正在使用:

SELECT * from foo ORDER BY RAND() LIMIT 2

从数据库中选择 2 个随机项目,但结果可能都是相同的项目,那么我怎样才能做到这一点,这样它们就不会并且总是不同呢?

一如既往,提前致谢:)

At present im using:

SELECT * from foo ORDER BY RAND() LIMIT 2

to select 2 random items from database but there is the possiblily of the result both being the same item so how can i make it so they are not and both always different?

As always thanks in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-11-04 19:37:48

使用ORDER BY RAND()不会导致重复的行。

从分散的论坛帖子和错误报告,这可能是以前的 MySQL 版本的情况(尽管我强烈怀疑这一点),但现在绝对不是了。我刚刚自己运行了一些测试,可以确认 ORDER BY RAND() 永远不会返回重复的行。

Using ORDER BY RAND() does not result in duplicate rows.

Judging from scattered forum posts and bug reports, this might have been the case in a previous MySQL version (although I strongly doubt this), but it definitely is not anymore. I have just run a couple of tests myself and can confirm that ORDER BY RAND() should never return duplicate rows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文