如何随机排序查询结果&选择随机行.(MySQL) [英] How to order results of a query randomly & select random rows. (MySQL)

查看:60
本文介绍了如何随机排序查询结果&选择随机行.(MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我是初学者.

我有两个问题:

1) 如何随机排序查询结果.

1) How can I order the results of a query randomly.

示例查询:

$get_questions = mysql_query("SELECT * FROM item_bank_tb WHERE item_type=1 OR item_type=3 OR item_type=4");

2) 从表中选择随机行的最佳方法.假设我想从表中随机抓取 10 行.

2) The best method to select random rows from a table. So lets say I want to grab 10 rows at random from a table.

非常感谢,

推荐答案

SELECT * FROM item_bank_tb WHERE item_type in(1,3,4) order by rand() limit 10

请注意,rand() 排序在大型记录集上非常慢.

Beware that order by rand() is very slow on large recordset.

编辑.看看这篇非常有趣的文章,它提出了一种不同的方法.

EDIT. Take a look at this very interesting article that presents a different approach.

http://explainextended.com/2009/03/01/selecting-随机行/

这篇关于如何随机排序查询结果&选择随机行.(MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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