如何使用Laravel计算特定表列 [英] How to count specific table column using laravel

查看:32
本文介绍了如何使用Laravel计算特定表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个Laravel查询:

I have here a laravel query:

DB::table('notifications')
    ->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
    ->select('notifications.data->via_domain as domain_name')
    ->groupBy('notifications.data->via_domain')
    ->get();

这显示表"notifications"和"domains"之间存在的域名.但是,我想按以下方式计算通知"中的notifications.notifiable_id字段:

This shows domain names that exists between the tables 'notifications' and 'domains'. However, I want to count notifications.notifiable_id field in 'notifications' by:

DB::table('notifications')
    ->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
    ->selectRaw('count(notifications.notifiable_id), notifications.data->via_domain')
    ->get();

但是它显示SQL语法错误.

However it shows error in SQL syntax.

(您的SQL语法有误;请查看对应于您的MySQL服务器版本以使用正确的语法靠近

(You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

通知中的

via_domain from notifications left join domains on
domains.domain = not at line 1 (SQL: select
count(notifications.notifiable_id), notifications.data->via_domain
from notifications left join domains on domains.domain =
notifications.data->'$."via_domain"')')

感谢您的帮助,明娜·桑.

Thanks for the help, minna-san.

推荐答案

我已经知道了!

这是我的代码:

DB::table('notifications')->leftJoin('domains', 'domains.domain', '=', 'notifications.data->via_domain')
                    ->select('notifications.data->via_domain as domainName', DB::raw('COUNT(notifications.notifiable_id) AS leadsCount'))
                    ->groupBy('notifications.data->via_domain')
                    ->get();

这将导致显示一个域名及其LeadsCount的数组.

This results to an array showing the domain name and its leadsCount.

谢谢大家.

这篇关于如何使用Laravel计算特定表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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