猿问

如何在 Laravel 查询中使用百分比

以下查询将产生 100 行:

$qtop = Quest::where('ttype',$id)      
                     ->where('country', $ucountry ) 
                     ->where('score', '>' , 240 ) 
                    ->orderby('score', 'desc')  
                    // ->take(25)
                    ->get();

而不是使用->take(25)我怎样才能占总行数的 25%?


喵喵时光机
浏览 162回答 2
2回答

尚方宝剑之说

在普通的 sql server 语法中,您可以使用TOP 25 PERCENTselect TOP 25 PERCENT * from table对于普通 mysql,您需要使用嵌套查询 @see Convert SQL Server query to MySQL:SELECT *FROM(&nbsp; &nbsp; SELECT table.*, @counter := @counter +1 counter&nbsp; &nbsp; FROM (select @counter:=0) initvar, table&nbsp; &nbsp; ORDER BY score) XWHERE X.counter <= (25/100 * @counter)ORDER BY score

慕田峪7331174

只需检查文档;$users&nbsp;=&nbsp;DB::table('users')->skip(10)->take(5)->get();https://laravel.com/docs/6.x/queries#ordering-grouping-limit-and-offset
随时随地看视频慕课网APP
我要回答