从mysql
数据库表中随机获取数据 其实,从数据库随机获取数据,很简单,只须用到一个rand()函数select * from table_name order by rand() limit 0,5下面
是一个小实例:从文章表中随机获取5条数据。$dblink=mysql_connect("localhost","root","123456")mysql_query("set names utf8")mysql_select_db("aixuexi") $sql="select * from waxx_article order by rand() limit 0,5" $rs=mysql_query($sql) while($row=mysql_fetch_array($rs)){$rows[]=$row } if($rows){foreach($rows as $v){?><div style="width:350pxheight:35pxline-height:35pxborder:1px solid #ccc"><?php echo $v['title']?></div><?php} }else{ echo "暂无文章" }首先程序产生一个
随机数,然后大于这个值的取10条
SELECT * FROM tablename WHERE id>xxx LIMIT 10
如果想分开的,那就多产生几个随机数x1 x2 x3
SELECT * FROM tablename WHERE id>x1 LIMIT 10
SELECT * FROM tablename WHERE id>x2 LIMIT 10
SELECT * FROM tablename WHERE id>x2 LIMIT 10
然后得到的记录再选10条
如果有其他索引的话也可以随机一个大于或小于这个
select * from tablename where uid>x1 and id>xxx limit 10
评论列表(0条)