sql查询到子查询的laravel查询

问题描述 投票:-3回答:1

请帮助如何使这个sql正确,不能使用两个选择和RAND

SELECT * FROM (SELECT * FROM `galleries` 
               WHERE domain_id = 13 AND is_deleted = 0 
               ORDER BY galleries.id DESC LIMIT 30) q 
ORDER BY RAND()

喜欢这个

$galleries = DB::table('galleries')->select( DB::raw('galleries.*') )
            ->where( 'domain_id', 13 )
            ->where( 'is_deleted', 0 ) ...
php jquery mysql laravel
1个回答
0
投票

您可以将查询重写为

SELECT * FROM `galleries`
WHERE domain_id = 13 
AND is_deleted = 0 
ORDER BY id DESC,RAND()
LIMIT 30

在laravel的东西

$galleries = DB::table('galleries')
            ->select( DB::raw('galleries.*') )
            ->where( 'domain_id', 13 )
            ->where( 'is_deleted', 0 )
            ->orderBy('id', 'desc')
            ->orderByRaw('RAND()')

或者从上面的查询中获得结果,而不使用->orderByRaw('RAND()')并执行shuffle()

$galleries = $galleries->toArray(); 
shuffle($galleries);
© www.soinside.com 2019 - 2024. All rights reserved.