以下查詢將產生 100 行:$qtop = Quest::where('ttype',$id)
->where('country', $ucountry )
->where('score', '>' , 240 )
->orderby('score', 'desc')
// ->take(25)
->get();而不是使用->take(25)我怎樣才能占總行數的 25%?
2 回答

尚方寶劍之說
TA貢獻1788條經驗 獲得超4個贊
在普通的 sql server 語法中,您可以使用TOP 25 PERCENT
select TOP 25 PERCENT * from table
對于普通 mysql,您需要使用嵌套查詢 @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

慕田峪7331174
TA貢獻1828條經驗 獲得超13個贊
只需檢查文檔;
$users = DB::table('users')->skip(10)->take(5)->get();
https://laravel.com/docs/6.x/queries#ordering-grouping-limit-and-offset
- 2 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
0/150
提交
取消