如何随机的从mysql表中取出一条记录?

 如何做效率才高呢?有时候代码少并不代表效率高,如果这题用到了 “order by rand()” 那就上了大当了

<?php 

$conn = mysql_connect('127.0.0.1','root','root') or die (mysql_error());
mysql_select_db('test');
mysql_query('SET NAMES UTF8');
$sql = " select count(1) as t from cj_question ";
$result = mysql_query($sql);
$data = mysql_fetch_assoc($result);

$count = empty($data) ? 0 : intval($data['t']);

if (empty($count))
{
die('表中没有数据');
}

$offset = mt_rand(0,$count-1);

$sql = " select * from cj_question limit $offset,1 ";

$result = mysql_query($sql);

$data = mysql_fetch_assoc($result);
var_dump($data);


你可能感兴趣的:(如何随机的从mysql表中取出一条记录?)