以下查询将产生 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 关注
- 161 浏览
添加回答
举报
0/150
提交
取消