2

The following query will result in 100 rows :

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

Instead of using ->take(25) how can I take 25% of the total rows ?

Abdallah Sakre
  • 915
  • 1
  • 9
  • 26
  • 1
    Does this answer your question? [Mysql Select only percentage of rows](https://stackoverflow.com/questions/22763342/mysql-select-only-percentage-of-rows) – piotr Jan 07 '20 at 22:49

2 Answers2

1

in plain sql server syntax you can use TOP 25 PERCENT

select TOP 25 PERCENT * from table

For plain mysql you need to use nested queries @see Convert SQL Server query to MySQL :

SELECT *
FROM
(
    SELECT table.*, @counter := @counter +1 counter
    FROM (select @counter:=0) initvar, table
    ORDER BY score
) X
WHERE X.counter <= (25/100 * @counter)
ORDER BY score
Mohamed23gharbi
  • 1,710
  • 23
  • 28
0

Just check the documentation;

$users = DB::table('users')->skip(10)->take(5)->get();

https://laravel.com/docs/6.x/queries#ordering-grouping-limit-and-offset