与groupby一样

发布于 2025-02-12 17:47:24 字数 737 浏览 0 评论 0原文

我有一个带有以下记录的用户表:

”“在此处输入图像描述”

从上面的记录中,我只想获取具有类似名称的用户。

我的预期结果是使用户具有类似名称的用户,如下图:

”在此处输入图像描述”

所以,这就是我尝试的:

$users = User::groupBy('name')->having(DB::raw('count(*)'), ">", "1")->select('name')->get();

dd($clients);

但是这是我得到的结果:

I have a users table with the following records:

enter image description here

From the above records, i want to fetch only users with like names.

My expected result is to get users with like names as seen in the screenshot below:

enter image description here

So, this is what I've tried:

$users = User::groupBy('name')->having(DB::raw('count(*)'), ">", "1")->select('name')->get();

dd($clients);

But this is the result I was getting:

enter image description here

I want to able to get the users with like names.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏尔 2025-02-19 17:47:24
    User::query()
   ->where('name', 'LIKE', "%{$searchTerm}%") 
   ->orWhere('email', 'LIKE', "%{$searchTerm}%") 
   ->get();

这将返回所有具有包含字符串$ searchTerm的名称或电子邮件的记录。

因此,就您而言,您可能还需要添加GroupBy语句。

您可以在链接

    User::query()
   ->where('name', 'LIKE', "%{$searchTerm}%") 
   ->orWhere('email', 'LIKE', "%{$searchTerm}%") 
   ->get();

This will return all records that have a name or email that contains the string in $searchTerm.

So in your case, you might need to add a groupBy statement too.

You can find more details in this link

离去的眼神 2025-02-19 17:47:24

我确实认为在您的情况下这应该有效:

    User::query()
        ->select('name')
        ->having(DB::raw('count(name)'), '>', 2)
        ->groupBy('name')
        ->get();

upp:

    $value = sprintf('%%%s%%', 'test');
    return User::query()
        ->select('name')
        ->where('name', 'like', $value)
        ->having(DB::raw('count(name)'), '>', 2)
        ->groupBy('name')
        ->get();

I do think this should work in your case:

    User::query()
        ->select('name')
        ->having(DB::raw('count(name)'), '>', 2)
        ->groupBy('name')
        ->get();

UPD:

    $value = sprintf('%%%s%%', 'test');
    return User::query()
        ->select('name')
        ->where('name', 'like', $value)
        ->having(DB::raw('count(name)'), '>', 2)
        ->groupBy('name')
        ->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文