本文介绍了Laravel Sum列数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试获取我的一个表中的int字段的总和应该很容易,但是不幸的是,并不是因为我使用Laravel,MySQL或Excel都得到了不同的结果。

Trying to get the sum of a int field in one of my table should be pretty easy, unfortunately it is not as I'm getting different result whether I use Laravel, MySQL or Excel.

Laravel 5.4给了我 20506

Laravel 5.4 gives me 20506:

Table::sum('field_name');

MySQL给了我 1830

Select sum(field_name) from table;

将Excel工作表中的数据导入数据库之前:
Sum给我 145689

And the data from the Excel sheet before importing it into the database:Sum gives me 145689

有什么想法吗?我尝试在进行总和之前将其强制转换为整数,但是它不起作用。
所有数字都很大,但是不包含逗号或点。

Any idea? I tried to cast to integer before doing the sum but it doesn't work.All the numbers are pretty big but don't contain comma or dot.

我必须总结的值示例:(有时包含空单元格)

Examples of values I have to sum: (contain sometimes empty cells)

17906774
99630157
28581131

159551532
20312892
668928885


推荐答案

$query = YourModel::query();
$query->withCount([
'activity AS yoursum' => function ($query) {
        $query->select(DB::raw("SUM(amount_total) as paidsum"))->where('status', 'paid');
    }
]);

这篇关于Laravel Sum列数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:01