【问题标题】:null returned intead of 0 on sum when not data in table laravel 5.8当表 laravel 5.8 中没有数据时,null 返回总和而不是 0
【发布时间】:2020-05-19 11:04:35
【问题描述】:

我的查询

 $summary['total_cash'] = DB::table('wallets')
                                  ->select(DB::raw('SUM(amount) as total_cash'))
                                  ->where('payment_details','affiliate product purchase commission')
                                  ->where('user_id',$group_id)
                                  ->first()->total_cash;

我得到的错误 https://prnt.sc/sjllwv

这些表中没有可用的数据

我要返回0

如有错别字请忘记

谢谢

【问题讨论】:

  • 请显示您想要返回的内容。您的查询返回多个列,因此 0 没有意义。我认为你真的应该在应用程序中处理这个问题。
  • 标题好像和你贴的代码和错误没有任何关系
  • @iainn ,查询已编辑,请立即查看
  • where('payment_details','affiliate product purchase commission') 是错误的。 payment_details 是否必须具有这 4 个值之一?如果是这样,请使用whereIn 方法。
  • @AkhtarMunir 是的,但我已更改为 0

标签: php mysql sql laravel eloquent


【解决方案1】:

如果您的数据库中默认金额字段为 NULL,请尝试此操作

$summary['total_cash'] = DB::table('wallets')
                         ->select(DB::raw('coalesce(SUM(amount),0) as total_cash'))
                         ->where('payment_details','affiliate product purchase commission')
                         ->where('user_id',$group_id)
                         ->first()->total_cash

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 2022-01-14
    • 2011-12-23
    • 2010-11-05
    相关资源
    最近更新 更多