netqqboy 2015-12-15 09:09:08 2083次浏览 1条回复 0 0 0

以下为从student_ids表中随机选取一条记录的mysql原生语句,请问用AR写法怎么写?谢谢!

SELECT *
FROM `student_ids` AS t1 
JOIN (  SELECT ROUND( RAND() * (  (SELECT MAX(id) FROM `student_ids`)-(SELECT MIN(id) FROM `student_ids`)  )+( SELECT MIN(id) FROM `student_ids` )  ) AS id  )
AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id LIMIT 1;
  • 回复于 2015-12-17 20:28 举报

    我给个思路吧,与其写这么复杂的不如如下操作
    A. 获取totalCount
    B. mt_rand (1, totalCount)
    C. 读取

    前提是你的pk是自增的或者有一定规律的。

您需要登录后才可以回复。登录 | 立即注册