如何以laravel形式将查询作为表格加入?

时间:2016-05-09 08:03:30

标签: sql laravel

像这样:

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;

1 个答案:

答案 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();