2 数据库中唯一的 RAND() 项
目前我正在使用:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
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.