如何在 Laravel 中获取非键列字段的不同值? [英] How to get distinct values for non-key column fields in Laravel?

查看:16
本文介绍了如何在 Laravel 中获取非键列字段的不同值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很容易,但不知道如何做.

This might be quite easy but have no idea how to.

我有一个表,可以为特定的非键列字段包含重复的值.如何使用 Query Builder 或 Eloquent 编写 SQL 查询,该查询将为该列获取具有不同值的行?

I have a table that can have repeated values for a particular non-key column field. How do I write a SQL query using Query Builder or Eloquent that will fetch rows with distinct values for that column?

请注意,我不是仅获取该列,它与其他列值结合使用,因此 distinct() 可能不会真正起作用.所以这个问题基本上可以是现在 distinct() 不接受任何参数,如何在查询中指定我想要区分的列?

Note that I am not fetching that column only, it is in conjunction with other column values, so distinct() might not really work. So that question basically can be how to specify the column that I want to be distinct in a query now that distinct() accepts no parameters?

推荐答案

你应该使用 groupby.在查询生成器中,您可以这样做:

You should use groupby. In Query Builder you can do it this way:

$users = DB::table('users')
            ->select('id','name', 'email')
            ->groupBy('name')
            ->get();

这篇关于如何在 Laravel 中获取非键列字段的不同值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆