mysql 抽取 随机取数据

mysql随机取一条数据

  1. 一条语句获取随机数据(防止取出)
select * from fa_record t1 RIGHT join (select ROUND(RAND()*(select max(id)-MIN(id) from fa_record)+(select MIN(id)-1 from fa_record)) rid) t2 on t1.id> t2.rid limit 1
  1. 分解语句
1. 获取最大和最小值
select max(id),MIN(id) from fa_record
获取maxId,minId
获取随机数
$rId = round(minId,maxId-1);
2. 获取随机数据
select * from fa_record id where id > $rId limit 1

你可能感兴趣的:(mysql 抽取 随机取数据)