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

laravel中分组查询前十条数据

WBOY
WBOYOriginal
2016-06-06 20:27:312888browse

查询的时候根据某个字段分组,然后查询出每组前几条数据,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>
Statement:
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