mysql随机查询一条数据

//主要使用ORDER BY RAND()

方法一:

SELECT aid FROM  `actions`  WHERE 1  ORDER BY RAND()  LIMIT 1

方法二:

$range_result = mysql_query( " SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM `table` ");
$range_row = mysql_fetch_object( $range_result );
$random = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM `table` WHERE `id` >= $random LIMIT 0,1 ");

方法三:
/**
floor函数是取整
rand() 生成0-1的随机数,包括0,包括1
count(*) 获取总数
*/

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset`,count(*) as count FROM `table` ");
$offset_row = mysql_fetch_object( $offset_result );
$offset = $offset_row->offset;

if($offset==0)
   $offset=1;

if($offset==$offset_row->count)
  $offset = $offset_row->count-1;

$result = mysql_query( " SELECT * FROM `table` LIMIT $offset, 1 " );

方法四:

SELECT * FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 1;