像这样:
SELECT * FROM `teachers` AS t1
JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `teachers`)-(SELECT MIN(id) FROM `teachers`))+(SELECT MIN(id) FROM `teachers`)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id LIMIT 20;
答案 0 :(得分:0)
你可以尝试加入raw。
Teacher::select(
'*',
)->join(
DB::raw(SELECT ROUND(RAND() * ((SELECT MAX(id) FROM `teachers`)-(SELECT MIN(id) FROM `teachers`))+(SELECT MIN(id) FROM `teachers`)) AS id) ) as t2, t1.id >= t2.id
)
->orderBy('t1.id')
->get();