gpt4 book ai didi

php - 为什么 distinct 不适用于 Laravel + Postgresql?

转载 作者:行者123 更新时间:2023-11-29 13:18:59 25 4
gpt4 key购买 nike

我想计算在ONLY ONE COLUMN 上具有不同值的行数。例如,如果我有 activities 表,其中有 email 列,我想计算 email 列中具有不同值的列数.

我正在使用 Laravel + PostgreSQL。我尝试了以下代码,但它正在计算所有行!

$emailsCount = DB::table('activities')
->where('user_id',$UserId)
->distinct('email')
->count();

后来,我开始知道 distinct() 不会接受任何参数,我试图找到解决我的问题的方法,有人(其他 Stackoverflow 问题)建议我使用 groupBy 就像下面的代码。

$emailsCount = DB::table('activities')
->where('user_id',$UserId)
->groupBy('email')
->count();

但是它返回了错误的信息。例如,在我的例子中,它返回 1 但我可以在 email 列中看到很多不同的电子邮件。我不知道出了什么问题。我怎样才能以正确的方式做到这一点?

最佳答案

正如您所说,distinct() 不要接受任何参数,相反您可以将该参数传递给 count 方法,如下所示:

$emailsCount = DB::table('activities') 
->where('user_id',$UserId)
->distinct()
->count(["email"]);

关于php - 为什么 distinct 不适用于 Laravel + Postgresql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45023173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com