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

查看:138
本文介绍了如何获取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天全站免登陆