如何从具有确切行数的表中选择随机行? [英] How to SELECT random rows from table with an exact number of row?

查看:74
本文介绍了如何从具有确切行数的表中选择随机行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP和MySQL,我只想从表中选择6行,而该表每天都有更多行.我尝试使用如下代码:

Using PHP and MySQL, I want to select only 6 rows from table which has more rows everyday. I try to use the code like:

  SELECT * FROM table WHERE rand()<=$fragment LIMIT 6

其中片段是6除以总行数.结果中的行数大部分为6,但有时少于6.

where fragment is 6 divided by number of total rows. The number of rows in result mostly be 6, but sometime less than 6.

如何获得正好有六行的结果?

How to get the result that have exactly six rows?

推荐答案

SELECT * FROM table 
WHERE some condition
ORDER BY RAND() 
LIMIT 6

这篇关于如何从具有确切行数的表中选择随机行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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