1

我需要在集合中返回以进行分页,但我只能在数组中返回,因为查询使用自然 JOIN。我如何使用 Laravel 集合进行此查询:

SELECT mensagens.* FROM mensagens NATURAL JOIN (SELECT id_emissor, MAX(created_at) created_at FROM mensagens WHERE id_receptor = ".$user_id." GROUP BY id_emissor) t

我用过这段代码:

$mensagem = \DB::select(DB::raw("SELECT mensagens.* FROM mensagens NATURAL JOIN (SELECT id_emissor, MAX(created_at) created_at FROM mensagens WHERE id_receptor = ".$user_id." GROUP BY id_emissor) t") );

4

1 回答 1

1

如果您想按原样使用查询,请像这样使用 -

\DB::raw("SELECT mensagens.* FROM mensagens NATURAL JOIN ( SELECT id_emissor, MAX(created_at) created_at FROM mensagens WHERE id_receptor = ".$user_id." GROUP BY id_emissor) t");

否则,您也可以使用 eloquent。

于 2017-09-15T07:31:32.020 回答