MySQL:如何检索一个随机行或多个随机行? [英] MySQL: How to retrieve a random row or multiple random rows?

查看:66
本文介绍了MySQL:如何检索一个随机行或多个随机行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL数据库表,用于存储照片的URL.我需要从特定类型的数据库中提取5条随机记录.我可以拉出5条这样的记录:

I have a MySQL database table that stores the URLs of photos. I need to pull 5 random records from the database of a particular type. I can pull 5 records like this:

SELECT Photos.* 
FROM Photos 
WHERE Photos.Type_ID = 4 
LIMIT 5

现在,我需要帮助尝试弄清楚每次如何提取不同的记录.如何从该结果集中检索随机行?

Now I need help trying to figure out how to pull different records every time. How can I retrieve random rows from this result set?

推荐答案

您可以使用ORDER BY RAND()来获取查询中的随机行.

You can use ORDER BY RAND() to get random rows in your query.

这篇关于MySQL:如何检索一个随机行或多个随机行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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