Home > Backend Development > PHP Tutorial > laravel中分组查询前十条数据

laravel中分组查询前十条数据

WBOY
Release: 2016-06-06 20:27:31
Original
2893 people have browsed it

查询的时候根据某个字段分组,然后查询出每组前几条数据,laravel这个语句怎么写 各位有什么思路吗?

回复内容:

查询的时候根据某个字段分组,然后查询出每组前几条数据,laravel这个语句怎么写 各位有什么思路吗?

<code class="php">use \App\User;
$result = User::limit(10)->groupBy('name');
//如果复杂的语句也可以直接sql(不推荐)
$result = DB::select('select id,name from users group by name limit 10');</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template