thinkphp5 sum聚合函数使用方法 sum使用where

2019-05-27  本文已影响0人  思议岁月

今天在做tp5项目的时候,使用sum聚合函数统计结果值一直为null,着落半天解决不了,最后找到原因,在此记录下。
项目是统计昨天的某字段和。
以下为原来代码

$yestoday=date('Y-m-d',strtotime('-1 day'));
$yuedu=Db::table('cs_fanwen')->where(['beifanwen_id'=>55,'time'=>$yestoday] )->sum('istixin');

原因是在时间比较的时候不能使用where,解决办法

$yestoday=date('Y-m-d',strtotime('-1 day'));
 $yuedu=Db::table('cs_fanwen')->where('beifanwen_id',55)->whereTime('time',$yestoday)->sum('istixin');

注意时间筛选使用whereTime
注意补充:经过自己筛选的知,当没有记录时访问结果为null,与上面无关。

上一篇 下一篇

猜你喜欢

热点阅读