[英]How can i get the sum of a MySQL table's field for today date?
I am building a simple shopping cart using PHP and as per title i want to get the sum (total earnings) of price
field of orders
table but only for today date.我正在使用 PHP 构建一个简单的购物车,根据标题,我想获得orders
表price
字段的总和(总收入),但仅限于今天。
I created the following function but i can get the total earnings for the last day there were earnings, not today.我创建了以下函数,但我可以获得最后一天的总收入,而不是今天。 For example if Today is Friday and the last day which there were earnings are Monday($180), then the result i get is Monday:$180 instead of Friday:$0 which is what i want.例如,如果今天是星期五,最后一天有收入是星期一(180 美元),那么我得到的结果是星期一:180 美元,而不是星期五:0 美元,这正是我想要的。
Here is what i have now:这是我现在拥有的:
public function getTotalEarningsToday()
{
$records = (new Query())->select([
'sum(price) as count',
"from_unixtime(updated_at, '%Y-%m-%d') as day",
])
->from(Orders::tableName())
->orderBy('day desc')
->groupBy('day')
->indexBy('day')
->limit(1)
->where(['status' => Orders::STATUS_COMPLETED])
->all();
return $records;
}
I tried to store the current date in a variable and do something like that:我试图将当前日期存储在变量中并执行以下操作:
public function getTotalEarningsToday()
{
$todayDate = date("d.m.Y H:i");
$records = (new Query())->select([
'sum(price) as count',
"from_unixtime(updated_at, '$todayDate') as day",
])
->from(Orders::tableName())
->orderBy('day desc')
->groupBy('day')
->indexBy('day')
->limit(1)
->where(['status' => Orders::STATUS_COMPLETED])
->all();
return $records;
}
and now i get the today date inside the Array but still i can't get the right sum earnings records:现在我得到了数组中的今天日期,但我仍然无法获得正确的总收入记录:
Array ( [count] => 61.35 [day] => 21.02.2022 09:18 ) )
It should be:它应该是:
Array ( [count] => 0 [day] => 21.02.2022 09:18 ) )
Any help would be very much appreciated.任何帮助将不胜感激。
I think you should use just day in this case, since time may vary.我认为在这种情况下你应该只使用一天,因为时间可能会有所不同。 If your time is stored in the date() format you can do it like this:如果您的时间以 date() 格式存储,您可以这样做:
$todayDate = strtotime(date("d.m.Y"));
$records = (new Query())->select([
'sum(price) as count',
'updated_at as day',
])
->from(Orders::tableName())
->orderBy('day desc')
->groupBy('day')
->indexBy('day')
->limit(1)
->where([
'AND',
'status' => Orders::STATUS_COMPLETED,
'updated_at' => $tadayDate]
)->all();
Or, as usual we have created_at
and updated_at
fields in the table, updated_at
may be empty, in this case, reasonable will be to use created_at
field to query results:或者,像往常一样我们在表中有created_at
和updated_at
字段, updated_at
可能为空,这种情况下,合理的会使用created_at
字段查询结果:
$todayDate = strtotime(date("d.m.Y"));
$records = (new Query())->select([
'sum(price) as count',
'created_at as day',
])
->from(Orders::tableName())
->orderBy('day desc')
->groupBy('day')
->indexBy('day')
->limit(1)
->where([
'AND',
'status' => Orders::STATUS_COMPLETED,
'created_at' => $tadayDate]
)->all();
Or use BETWEEN
for the date, like it was mentioned in the comments.或者使用BETWEEN
作为日期,就像评论中提到的那样。
updated_at >= '2022-02-21 00:00' AND updated_at <= '2022-02-21 23:59'
There is no need to use group by, order by, index by or limit when all you want is to get sum over one day.当您只想获得一天的总和时,无需使用 group by、order by、index by 或 limit。
$sum = Orders::find()
->where(new \yii\db\Expression(
'DATE(FROM_UNIXTIME(`updated_at`)) = :today',
[':today' => date('Y-m-d')]
))->sum('price');
This one works for me这个对我有用
$today = date("Y-m-d");
$queue = Orders::find()
->select(['SUM(price) as total'])
->where(['DATE(created_at)' => $today])
->one();
return $queue['total'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.